共用方式為


Azure Storage Data Movement Blobs client library for .NET - version 12.0.0-beta.6

Server Version: 2020-04-08, 2020-02-10, 2019-12-12, 2019-07-07, and 2020-02-02

Project Status: Beta

This product is in beta. Some features will be missing or have significant bugs. Please see Known Issues for detailed information.


Azure Storage is a Microsoft-managed service providing cloud storage that is highly available, secure, durable, scalable, and redundant. Azure Storage includes Azure Blobs (objects), Azure Data Lake Storage Gen2, Azure Files, and Azure Queues.

The Azure Storage Data Movement library is optimized for uploading, downloading and copying customer data.

The Azure.Storage.DataMovement.Blobs library provides infrastructure shared by the other Azure Storage client libraries.

Source code | Package (NuGet) | API reference documentation | REST API documentation | Product documentation

Getting started

Install the package

Install the Azure Storage client library for .NET you'd like to use with NuGet and the Azure.Storage.DataMovement.Blobs client library will be included:

dotnet add package Azure.Storage.DataMovement --prerelease
dotnet add package Azure.Storage.DataMovement.Blobs --prerelease

Prerequisites

You need an Azure subscription and a Storage Account to use this package.

To create a new Storage Account, you can use the Azure Portal, Azure PowerShell, or the Azure CLI. Here's an example using the Azure CLI:

az storage account create --name MyStorageAccount --resource-group MyResourceGroup --location westus --sku Standard_LRS

Authenticate the client

The Azure.Storage.DataMovement.Blobs library uses clients from the Azure.Storage.Blobs package to communicate with the Azure Blob Storage service. For more information see the Azure.Storage.Blobs authentication documentation.

Key concepts

The Azure Storage Common client library contains shared infrastructure like [authentication credentials][auth_credentials] and RequestFailedException.

Thread safety

We guarantee that all client instance methods are thread-safe and independent of each other (guideline). This ensures that the recommendation of reusing client instances is always safe, even across threads.

Additional concepts

Client options | Accessing the response | Long-running operations | Handling failures | Diagnostics | Mocking | Client lifetime

Examples

This section demonstrates usage of Data Movement for interacting with blob storage.

Extensions on BlobContainerClient

For applications with preexisting code using Azure.Storage.Blobs, this package provides extension methods for BlobContainerClient to get some of the benefits of the TransferManager with minimal extra code.

Instantiate the BlobContainerClient

BlobServiceClient service = new BlobServiceClient(serviceUri, credential);

BlobContainerClient container = service.GetBlobContainerClient(containerName);

Upload a local directory to the root of the container

DataTransfer transfer = await container.StartUploadDirectoryAsync(localPath);

await transfer.WaitForCompletionAsync();

Upload a local directory to a virtual directory in the container by specifying a directory prefix

DataTransfer transfer = await container.StartUploadDirectoryAsync(localPath, blobDirectoryPrefix);

await transfer.WaitForCompletionAsync();

Upload a local directory to a virtual directory in the container specifying more advanced options

BlobContainerClientTransferOptions options = new BlobContainerClientTransferOptions
{
    BlobContainerOptions = new BlobStorageResourceContainerOptions
    {
        BlobDirectoryPrefix = blobDirectoryPrefix
    },
    TransferOptions = new DataTransferOptions()
    {
        CreationPreference = StorageResourceCreationPreference.OverwriteIfExists,
    }
};

DataTransfer transfer = await container.StartUploadDirectoryAsync(localPath, options);

await transfer.WaitForCompletionAsync();

Download the entire container to a local directory

DataTransfer transfer = await container.StartDownloadToDirectoryAsync(localDirectoryPath);

await transfer.WaitForCompletionAsync();

Download a directory in the container by specifying a directory prefix

DataTransfer tranfer = await container.StartDownloadToDirectoryAsync(localDirectoryPath2, blobDirectoryPrefix);

await tranfer.WaitForCompletionAsync();

Download from the container specifying more advanced options

BlobContainerClientTransferOptions options = new BlobContainerClientTransferOptions
{
    BlobContainerOptions = new BlobStorageResourceContainerOptions
    {
        BlobDirectoryPrefix = blobDirectoryPrefix
    },
    TransferOptions = new DataTransferOptions()
    {
        CreationPreference = StorageResourceCreationPreference.OverwriteIfExists,
    }
};

DataTransfer tranfer = await container.StartDownloadToDirectoryAsync(localDirectoryPath2, options);

await tranfer.WaitForCompletionAsync();

Initializing Blob Storage StorageResource

Azure.Storage.DataMovement.Blobs exposes BlobsStorageResourceProvider to create StorageResource instances for each type of blob (block, page, append) as well as a blob container. The resource provider should be initialized with a credential to properly authenticate the storage resources. The following demonstrates this using an Azure.Core token credential.

BlobsStorageResourceProvider blobs = new(tokenCredential);

To create a blob StorageResource, use the methods FromBlob or FromContainer.

StorageResource container = blobs.FromContainer(
    new Uri("http://myaccount.blob.core.windows.net/container"));

// Block blobs are the default if no options are specified
StorageResource blockBlob = blobs.FromBlob(
    new Uri("http://myaccount.blob.core.windows.net/container/sample-blob-block"),
    new BlockBlobStorageResourceOptions());
StorageResource pageBlob = blobs.FromBlob(
    new Uri("http://myaccount.blob.core.windows.net/container/sample-blob-page"),
    new PageBlobStorageResourceOptions());
StorageResource appendBlob = blobs.FromBlob(
    new Uri("http://myaccount.blob.core.windows.net/container/sample-blob-append"),
    new AppendBlobStorageResourceOptions());

Storage resources can also be initialized with the appropriate client object from Azure.Storage.Blobs. Since these resources will use the credential already present in the client object, no credential is required in the provider when using FromClient(). However, a BlobsStorageResourceProvider must still have a credential if it is to be used in TransferManagerOptions for resuming a transfer.

BlobsStorageResourceProvider blobs = new();
StorageResource containerResource = blobs.FromClient(blobContainerClient);
StorageResource blockBlobResource = blobs.FromClient(blockBlobClient);
StorageResource pageBlobResource = blobs.FromClient(pageBlobClient);
StorageResource appendBlobResource = blobs.FromClient(appendBlobClient);

There are more options which can be used when creating a blob storage resource. Below are some examples.

BlobStorageResourceContainerOptions virtualDirectoryOptions = new()
{
    BlobDirectoryPrefix = "blob/directory/prefix"
};

StorageResource virtualDirectoryResource = blobs.FromClient(
    blobContainerClient,
    virtualDirectoryOptions);
BlockBlobStorageResourceOptions resourceOptions = new()
{
    Metadata = new DataTransferProperty<IDictionary<string, string>> (
        new Dictionary<string, string>
        {
            { "key", "value" }
        })
};
StorageResource leasedBlockBlobResource = blobs.FromClient(
    blockBlobClient,
    resourceOptions);

Upload

An upload takes place between a local file StorageResource as source and blob StorageResource as destination.

Upload a block blob.

DataTransfer dataTransfer = await transferManager.StartTransferAsync(
    sourceResource: files.FromFile(sourceLocalPath),
    destinationResource: blobs.FromBlob(destinationBlobUri));
await dataTransfer.WaitForCompletionAsync();

Upload a directory as a specific blob type.

DataTransfer dataTransfer = await transferManager.StartTransferAsync(
    sourceResource: files.FromDirectory(sourcePath),
    destinationResource: blobs.FromContainer(
        blobContainerUri,
        new BlobStorageResourceContainerOptions()
        {
            // Block blobs are the default if not specified
            BlobType = new(BlobType.Block),
            BlobDirectoryPrefix = optionalDestinationPrefix,
        }));

Download

A download takes place between a blob StorageResource as source and local file StorageResource as destination.

Download a blob.

DataTransfer dataTransfer = await transferManager.StartTransferAsync(
    sourceResource: blobs.FromBlob(sourceBlobUri),
    destinationResource: files.FromFile(downloadPath));
await dataTransfer.WaitForCompletionAsync();

Download a container which may contain a mix of blob types.

DataTransfer dataTransfer = await transferManager.StartTransferAsync(
    sourceResource: blobs.FromContainer(
        blobContainerUri,
        new BlobStorageResourceContainerOptions()
        {
            BlobDirectoryPrefix = optionalSourcePrefix
        }),
    destinationResource: files.FromDirectory(downloadPath));
await dataTransfer.WaitForCompletionAsync();

Blob Copy

A copy takes place between two blob StorageResource instances. Copying between to Azure blobs uses PUT from URL REST APIs, which do not transfer data through the machine running DataMovement.

Copy a single blob. Note the destination blob is an append blob, regardless of the first blob's type.

DataTransfer dataTransfer = await transferManager.StartTransferAsync(
    sourceResource: blobs.FromBlob(sourceBlobUri),
    destinationResource: blobs.FromBlob(destinationBlobUri, new AppendBlobStorageResourceOptions()));
await dataTransfer.WaitForCompletionAsync();

Copy a blob container.

DataTransfer dataTransfer = await transferManager.StartTransferAsync(
sourceResource: blobs.FromContainer(
    sourceContainerUri,
    new BlobStorageResourceContainerOptions()
    {
        BlobDirectoryPrefix = sourceDirectoryName
    }),
destinationResource: blobs.FromContainer(
    destinationContainerUri,
    new BlobStorageResourceContainerOptions()
    {
        // all source blobs will be copied as a single type of destination blob
        // defaults to block blobs if unspecified
        BlobType = new(BlobType.Block),
        BlobDirectoryPrefix = downloadPath
    }));
await dataTransfer.WaitForCompletionAsync();

Troubleshooting

TODO

Next steps

TODO

Contributing

See the Storage CONTRIBUTING.md for details on building, testing, and contributing to these libraries.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Impressions