public async Task PathClient_CanGetParentDirectoryClient()
        {
            // Arrange
            var parentDirName = SharesClientBuilder.GetNewDirectoryName();

            await using DisposingShare test = await SharesClientBuilder.GetTestShareAsync();

            ShareDirectoryClient parentDirClient = InstrumentClient(test.Container
                                                                    .GetRootDirectoryClient()
                                                                    .GetSubdirectoryClient(parentDirName));
            await parentDirClient.CreateAsync();

            ShareFileClient fileClient = InstrumentClient(parentDirClient
                                                          .GetFileClient(SharesClientBuilder.GetNewFileName()));
            await fileClient.CreateAsync(Constants.KB);

            // Act
            parentDirClient = fileClient.GetParentShareDirectoryClient();
            // make sure that client is functional
            var dirProperties = await parentDirClient.GetPropertiesAsync();

            // Assert
            Assert.AreEqual(fileClient.Path.GetParentPath(), parentDirClient.Path);
            Assert.AreEqual(fileClient.AccountName, parentDirClient.AccountName);
            Assert.IsNotNull(dirProperties);
        }
Exemplo n.º 2
0
        public async Task UploadAsync(ConnectorConfig config, string fullFileName, MemoryStream stream)
        {
            var dirName  = Path.GetDirectoryName(fullFileName).ToString();
            var fileName = Path.GetFileName(fullFileName).ToString();

            ShareClient share = new ShareClient(config.connectionString, config.shareName);

            if (!share.Exists())
            {
                await share.CreateAsync();
            }

            ShareDirectoryClient directory = share.GetDirectoryClient(dirName);

            if (!directory.Exists())
            {
                await directory.CreateAsync();
            }

            ShareFileClient file = directory.GetFileClient(fileName);
            await file.CreateAsync(stream.Length);

            await file.UploadAsync(stream);

            /*
             * await file.UploadRange(
             *      ShareFileRangeWriteType.Update,
             *      new HttpRange(0, stream.Length),
             *      stream);*/
        }
Exemplo n.º 3
0
        public async Task UploadRangeFromUriAsync_SourceBearerTokenFail()
        {
            // Arrange
            BlobServiceClient blobServiceClient = InstrumentClient(new BlobServiceClient(
                                                                       new Uri(Tenants.TestConfigOAuth.BlobServiceEndpoint),
                                                                       Tenants.GetOAuthCredential(Tenants.TestConfigOAuth),
                                                                       GetBlobOptions()));
            BlobContainerClient containerClient = InstrumentClient(blobServiceClient.GetBlobContainerClient(GetNewShareName()));

            try
            {
                await containerClient.CreateIfNotExistsAsync();

                AppendBlobClient appendBlobClient = InstrumentClient(containerClient.GetAppendBlobClient(GetNewFileName()));
                await appendBlobClient.CreateAsync();

                byte[] data = GetRandomBuffer(Constants.KB);
                using Stream stream = new MemoryStream(data);
                await appendBlobClient.AppendBlockAsync(stream);

                ShareServiceClient serviceClient = SharesClientBuilder.GetServiceClient_OAuthAccount_SharedKey();
                await using DisposingShare test = await GetTestShareAsync(
                                service : serviceClient,
                                shareName : GetNewShareName());

                ShareDirectoryClient directoryClient = InstrumentClient(test.Share.GetDirectoryClient(GetNewDirectoryName()));
                await directoryClient.CreateAsync();

                ShareFileClient fileClient = InstrumentClient(directoryClient.GetFileClient(GetNewFileName()));
                await fileClient.CreateAsync(Constants.KB);

                HttpAuthorization sourceAuthHeader = new HttpAuthorization(
                    "Bearer",
                    "auth token");

                ShareFileUploadRangeFromUriOptions options = new ShareFileUploadRangeFromUriOptions
                {
                    SourceAuthentication = sourceAuthHeader
                };

                HttpRange range = new HttpRange(0, Constants.KB);

                // Act
                await TestHelper.AssertExpectedExceptionAsync <RequestFailedException>(
                    fileClient.UploadRangeFromUriAsync(
                        sourceUri: appendBlobClient.Uri,
                        range: range,
                        sourceRange: range,
                        options: options),
                    e => Assert.AreEqual("CannotVerifyCopySource", e.ErrorCode));
            }
            finally
            {
                await containerClient.DeleteIfExistsAsync();
            }
        }
        public async Task UploadRangeFromUriAsync_SourceBearerToken()
        {
            // Arrange
            BlobServiceClient blobServiceClient = InstrumentClient(new BlobServiceClient(
                                                                       new Uri(TestConfigOAuth.BlobServiceEndpoint),
                                                                       GetOAuthCredential(TestConfigOAuth),
                                                                       GetBlobOptions()));
            BlobContainerClient containerClient = InstrumentClient(blobServiceClient.GetBlobContainerClient(GetNewShareName()));

            try
            {
                await containerClient.CreateIfNotExistsAsync();

                AppendBlobClient appendBlobClient = InstrumentClient(containerClient.GetAppendBlobClient(GetNewFileName()));
                await appendBlobClient.CreateAsync();

                byte[] data = GetRandomBuffer(Constants.KB);
                using Stream stream = new MemoryStream(data);
                await appendBlobClient.AppendBlockAsync(stream);

                ShareServiceClient serviceClient = GetServiceClient_OAuth_SharedKey();
                await using DisposingShare test = await GetTestShareAsync(
                                service : serviceClient,
                                shareName : GetNewShareName());

                ShareDirectoryClient directoryClient = InstrumentClient(test.Share.GetDirectoryClient(GetNewDirectoryName()));
                await directoryClient.CreateAsync();

                ShareFileClient fileClient = InstrumentClient(directoryClient.GetFileClient(GetNewFileName()));
                await fileClient.CreateAsync(Constants.KB);

                string sourceBearerToken = await GetAuthToken();

                HttpAuthorization sourceAuthHeader = new HttpAuthorization(
                    "Bearer",
                    sourceBearerToken);

                ShareFileUploadRangeFromUriOptions options = new ShareFileUploadRangeFromUriOptions
                {
                    SourceAuthentication = sourceAuthHeader
                };

                HttpRange range = new HttpRange(0, Constants.KB);

                // Act
                await fileClient.UploadRangeFromUriAsync(
                    sourceUri : appendBlobClient.Uri,
                    range : range,
                    sourceRange : range,
                    options : options);
            }
            finally
            {
                await containerClient.DeleteIfExistsAsync();
            }
        }
Exemplo n.º 5
0
        public override async Task SetupAsync()
        {
            await base.SetupAsync();

            // See https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-shares--directories--files--and-metadata for
            // restrictions on file share naming.
            _shareClient = new ShareClient(PerfTestEnvironment.Instance.FileSharesConnectionString, Guid.NewGuid().ToString());
            await _shareClient.CreateAsync();

            ShareDirectoryClient DirectoryClient = _shareClient.GetDirectoryClient(Path.GetRandomFileName());
            await DirectoryClient.CreateAsync();

            _fileClient = DirectoryClient.GetFileClient(Path.GetRandomFileName());
            await _fileClient.CreateAsync(_stream.Length, cancellationToken : CancellationToken.None);
        }
        public async Task Sample01b_HelloWorldAsync_Download()
        {
            string      originalPath  = CreateTempFile(SampleFileContent);
            string      localFilePath = CreateTempPath();
            string      shareName     = Randomize("sample-share");
            ShareClient share         = new ShareClient(ConnectionString, shareName);

            try
            {
                await share.CreateAsync();

                ShareDirectoryClient directory = share.GetDirectoryClient("sample-dir");
                await directory.CreateAsync();

                ShareFileClient file = directory.GetFileClient("sample-file");

                // Upload the file
                using (FileStream stream = File.OpenRead(originalPath))
                {
                    await file.CreateAsync(stream.Length);

                    await file.UploadRangeAsync(
                        ShareFileRangeWriteType.Update,
                        new HttpRange(0, stream.Length),
                        stream);
                }

                await Sample01b_HelloWorldAsync.DownloadAsync(ConnectionString, shareName, localFilePath);

                // Verify the contents
                Assert.AreEqual(SampleFileContent, File.ReadAllText(localFilePath));
            }
            finally
            {
                await share.DeleteAsync();

                File.Delete(originalPath);
                File.Delete(localFilePath);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Create a share and upload a file.
        /// </summary>
        /// <param name="connectionString">
        /// A connection string to your Azure Storage account.
        /// </param>
        /// <param name="shareName">
        /// The name of the share to create and upload to.
        /// </param>
        /// <param name="localFilePath">
        /// Path of the file to upload.
        /// </param>
        public static async Task UploadAsync(string connectionString, string shareName, string localFilePath)
        {
            // Get a connection string to our Azure Storage account.  You can
            // obtain your connection string from the Azure Portal (click
            // Access Keys under Settings in the Portal Storage account blade)
            // or using the Azure CLI with:
            //
            //     az storage account show-connection-string --name <account_name> --resource-group <resource_group>
            //
            // And you can provide the connection string to your application
            // using an environment variable.

            // Name of the directory and file we'll create
            string dirName  = "sample-dir";
            string fileName = "sample-file";

            // Get a reference to a share and then create it
            ShareClient share = new ShareClient(connectionString, shareName);
            await share.CreateAsync();

            // Get a reference to a directory and create it
            ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
            await directory.CreateAsync();

            // Get a reference to a file and upload it
            ShareFileClient file = directory.GetFileClient(fileName);

            using (FileStream stream = File.OpenRead(localFilePath))
            {
                await file.CreateAsync(stream.Length);

                await file.UploadRangeAsync(
                    ShareFileRangeWriteType.Update,
                    new HttpRange(0, stream.Length),
                    stream);
            }
        }
Exemplo n.º 8
0
            public static async Task <DisposingDirectory> CreateAsync(DisposingShare test, ShareDirectoryClient directory)
            {
                await directory.CreateAsync();

                return(new DisposingDirectory(test, directory));
            }