Exemplo n.º 1
0
        private async Task InitAsync(Access access)
        {
            await _semaphoreSlim.WaitAsync();

            try
            {
                if (_isInitialized)
                {
                    return;
                }

                _access       = access;
                BucketService = new BucketService(_access);
                ObjectService = new ObjectService(_access);
                Bucket        = await BucketService.GetBucketAsync("nutz-mich");

                _isInitialized = true;
            }
            catch (Exception ex)
            {
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }
Exemplo n.º 2
0
        public async Task <int> UploadLargeFilesToCloud()
        {
            #region "for large file upload; but the below code doesn't work porperly as it cann't append data at tardigrade server side;"
            var restrictedBucketService = new BucketService(access);

            var newBucketName = "flureebucket";
            var file          = await GetFlureeSnapshot("file.mp4");

            var bucket = await restrictedBucketService.GetBucketAsync(newBucketName);

            using Stream source = File.OpenRead(file.FileName);
            int    chunkSize  = 4 * 1024 * 1024;
            long   uploadSize = chunkSize;
            byte[] buffer     = new byte[chunkSize];
            int    bytesRead;
            while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
            {
                var uploadOperationRestricted = await objectService.UploadObjectAsync(bucket, file.FileName, new UploadOptions(), buffer, true);

                uploadOperationRestricted.UploadOperationProgressChanged += UploadOperationRestricted_UploadOperationProgressChanged;
                uploadOperationRestricted.UploadOperationEnded           += UploadOperationRestricted_UploadOperationEnded;
                await uploadOperationRestricted.StartUploadAsync();

                var mb = ((float)uploadSize / (float)file.File.Length) * 100;
                Console.WriteLine($"{mb} % uploaded");
                uploadSize += chunkSize;
            }

            return(0);

            #endregion
        }
Exemplo n.º 3
0
        public async Task AccessShare_Creates_UsableSharedAccessForDownload()
        {
            string serializedAccess;
            string bucketname = "accessshare-creates-usablesharedaccessfordownload";

            byte[] bytesToUpload = ObjectServiceTest.GetRandomBytes(2048);

            using (Access scope = new Access(TestConstants.SATELLITE_URL, TestConstants.VALID_API_KEY, TestConstants.ENCRYPTION_SECRET))
            {
                await _bucketService.CreateBucketAsync(bucketname);

                var bucket = await _bucketService.GetBucketAsync(bucketname);

                var uploadOperation = await _objectService.UploadObjectAsync(bucket, "test/test-file", new UploadOptions(), bytesToUpload, false);

                await uploadOperation.StartUploadAsync();

                Permission permission = new Permission();
                permission.AllowUpload   = false; //Should not change anything as we are downloading here
                permission.AllowDownload = true;
                List <SharePrefix> sharePrefixes = new List <SharePrefix>();
                sharePrefixes.Add(new SharePrefix()
                {
                    Bucket = bucketname, Prefix = "test/"
                });
                var restricted = scope.Share(permission, sharePrefixes);
                serializedAccess = restricted.Serialize();
            }

            await Task.Delay(SATELLITE_WAIT_DURATION); //Wait a bit so that some things can happen on the satellite

            Access restrictedEnv;

            try
            {
                restrictedEnv = new Access(serializedAccess);
            }
            catch
            {
                Assert.Fail("Failed to create restricted scope from serialized scope");
                return;
            }

            var restrictedObjectService = new ObjectService(restrictedEnv);
            var restrictedBucketService = new BucketService(restrictedEnv);
            var restrictedBucket        = await restrictedBucketService.GetBucketAsync(bucketname);

            var downloadOperation = await restrictedObjectService.DownloadObjectAsync(restrictedBucket, "test/test-file", new DownloadOptions(), false);

            await downloadOperation.StartDownloadAsync();

            Assert.IsTrue(downloadOperation.Completed);
            Assert.AreEqual(bytesToUpload.Length, downloadOperation.BytesReceived);

            for (int i = 0; i < bytesToUpload.Length; i++)
            {
                Assert.AreEqual(bytesToUpload[i], downloadOperation.DownloadedBytes[i], "DownloadedBytes are not equal at index " + i);
            }
        }
Exemplo n.º 4
0
        public async Task RevokeAccess_MakesAccesUnusable()
        {
            string serializedAccess;
            string bucketname = "revoke-access-makes-access-unusable";

            byte[] bytesToUpload = ObjectServiceTest.GetRandomBytes(2048);

            using (Access scope = new Access(TestConstants.SATELLITE_URL, TestConstants.VALID_API_KEY, TestConstants.ENCRYPTION_SECRET))
            {
                await _bucketService.CreateBucketAsync(bucketname);

                Permission permission = new Permission();
                permission.AllowUpload = true;
                List <SharePrefix> sharePrefixes = new List <SharePrefix>();
                sharePrefixes.Add(new SharePrefix()
                {
                    Bucket = bucketname, Prefix = "test/"
                });
                var restricted = scope.Share(permission, sharePrefixes);
                serializedAccess = restricted.Serialize();

                await Task.Delay(SATELLITE_WAIT_DURATION); //Wait a bit so that some things can happen on the satellite

                Access restrictedEnv;
                try
                {
                    restrictedEnv = new Access(serializedAccess);
                }
                catch
                {
                    Assert.Fail("Failed to create restricted scope from serialized scope");
                    return;
                }

                var restrictedObjectService = new ObjectService(restrictedEnv);
                var restrictedBucketService = new BucketService(restrictedEnv);
                var restrictedBucket        = await restrictedBucketService.GetBucketAsync(bucketname);

                var uploadOperationRestricted = await restrictedObjectService.UploadObjectAsync(restrictedBucket, "test/subfolder/test-file-upload", new UploadOptions(), bytesToUpload, false);

                await uploadOperationRestricted.StartUploadAsync();

                Assert.IsTrue(uploadOperationRestricted.Completed);
                Assert.AreEqual(bytesToUpload.Length, uploadOperationRestricted.BytesSent);

                //Revoke access
                await scope.RevokeAsync(restrictedEnv);

                //Try uploading again
                var uploadOperationRestricted2 = await restrictedObjectService.UploadObjectAsync(restrictedBucket, "test/subfolder/test-file-upload", new UploadOptions(), bytesToUpload, false);

                await uploadOperationRestricted2.StartUploadAsync();

                Assert.IsFalse(uploadOperationRestricted2.Completed);
                Assert.IsTrue(uploadOperationRestricted2.Failed);
            }
        }
Exemplo n.º 5
0
        public async Task AccessShare_Creates_UsableSharedAccessForUploadDeep()
        {
            string serializedAccess;
            string bucketname = "accessshare-creates-usablesharedaccessforuploaddeep";

            byte[] bytesToUpload = ObjectServiceTest.GetRandomBytes(2048);

            using (Access scope = new Access(TestConstants.SATELLITE_URL, TestConstants.VALID_API_KEY, TestConstants.ENCRYPTION_SECRET))
            {
                await _bucketService.CreateBucketAsync(bucketname);

                Permission permission = new Permission();
                permission.AllowUpload   = true;
                permission.AllowDownload = false; //should not change anything as we are uploading here
                List <SharePrefix> sharePrefixes = new List <SharePrefix>();
                sharePrefixes.Add(new SharePrefix()
                {
                    Bucket = bucketname, Prefix = "test/subfolder/"
                });
                var restricted = scope.Share(permission, sharePrefixes);
                serializedAccess = restricted.Serialize();
            }

            await Task.Delay(1500); //Wait a bit so that some things can happen on the satellite

            Access restrictedEnv;

            try
            {
                restrictedEnv = new Access(serializedAccess);
            }
            catch
            {
                Assert.Fail("Failed to create restricted scope from serialized scope");
                return;
            }

            var restrictedObjectService = new ObjectService(restrictedEnv);
            var restrictedBucketService = new BucketService(restrictedEnv);
            var restrictedBucket        = await restrictedBucketService.GetBucketAsync(bucketname);

            var uploadOperationRestricted = await restrictedObjectService.UploadObjectAsync(restrictedBucket, "test/subfolder/test-file-upload", new UploadOptions(), bytesToUpload, false);

            await uploadOperationRestricted.StartUploadAsync();

            Assert.IsTrue(uploadOperationRestricted.Completed);
            Assert.AreEqual(bytesToUpload.Length, uploadOperationRestricted.BytesSent);
        }
Exemplo n.º 6
0
        public async Task <int> UploadToCloud()
        {
            // Listing buckets.
            var bucketService = new BucketService(access);

            var buckets = await bucketService.ListBucketsAsync(new ListBucketsOptions());

            foreach (var b in buckets.Items)
            {
                Console.WriteLine(b.Name);
            }
            var restrictedBucketService = new BucketService(access);

            var newBucketName = "flureebucket";

            var bucket = await restrictedBucketService.GetBucketAsync(newBucketName);

            var fileObject = await GetFlureeSnapshot("1585578518736.avro");

            var uploadOperationRestricted = await objectService.UploadObjectAsync(bucket, fileObject.FileName, new UploadOptions(), fileObject.File, true);

            uploadOperationRestricted.UploadOperationProgressChanged += UploadOperationRestricted_UploadOperationProgressChanged;
            uploadOperationRestricted.UploadOperationEnded           += UploadOperationRestricted_UploadOperationEnded;
            await uploadOperationRestricted.StartUploadAsync();


            // Download a file from a bucket.

            var objects = await objectService.ListObjectsAsync(bucket, new ListObjectsOptions());

            foreach (var obj in objects.Items)
            {
                //work with the objects
                Console.WriteLine($"Found {obj.Key} {obj.SystemMetaData.Created}");


                //await objectService.DeleteObjectAsync(bucket, obj.Path);

//                var downloadSvc = await objectService.DownloadObjectAsync(bucket, obj.Path, false);
//                downloadSvc.DownloadOperationProgressChanged += DownloadOperation_DownloadOperationProgressChanged;
//                downloadSvc.DownloadOperationEnded += DownloadOperation_DownloadOperationEnded;
//                await downloadSvc.StartDownloadAsync();
            }


            return(0);
        }