使用@azure/storage-blob 将 blob 从一个存储帐户复制到另一个存储帐户

Copy blob from one storage account to another using @azure/storage-blob

使用@azure/storage-blob 将 blob 从一个存储帐户复制到另一个存储帐户的最佳方法是什么?

我认为最好使用流而不是先下载再上传,但想知道下面的代码是否是使用流的 correct/optimal 实现。

const srcCredential = new ClientSecretCredential(<src-ten-id>, <src-client-id>, <src-secret>);
const destCredential = new ClientSecretCredential(<dest-ten-id>, <dest-client-id>, <dest-secret>);

const srcBlobClient = new BlobServiceClient(<source-blob-url>, srcCredential);
const destBlobClient = new BlobServiceClient(<dest-blob-url>, destCredential);

const sourceContainer = srcBlobClient.getContainerClient("src-container");
const destContainer = destBlobClient.getContainerClient("dest-container");

const sourceBlob = sourceContainer.getBlockBlobClient("blob");
const destBlob = destContainer.getBlockBlobClient(sourceBlob.name)

// copy blob
await destBlob.uploadStream((await sourceBlob.download()).readableStreamBody);

您当前的方法是下载源 blob,然后重新上传,这并不是最佳选择。

更好的方法是使用 async copy blob。您要使用的方法是 beginCopyFromURL(string, BlobBeginCopyFromURLOptions). You would need to create a Shared Access Signature URL on the source blob with at least Read permission. You can use generateBlobSASQueryParameters SDK 方法来创建它。

const sourceBlob = sourceContainer.getBlockBlobClient("blob");
const destBlob = destContainer.getBlockBlobClient(sourceBlob.name);

const sourceBlobSasUrl = GenerateSasUrlWithReadPermissionOnSourceBlob(sourceBlob);
// copy blob
await destBlob.beginCopyFromURL(sourceBlobSasUrl);