在Azure Blob容器之间复制文件

nbysray5  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(105)

我正在使用@azure/storage-blob包来操作Azure中的文件。
在同一个Azure存储帐户中,我有两个存储容器,即源和目的地。
我尝试做的是将位于源容器中的文件复制到目标容器,而无需下载该文件。
我正在下载到缓冲区并上传到目的地。
有没有办法直接做到这一点?

const blobServiceClient = BlobServiceClient.fromConnectionString(BLOB_CONNECTION_STRING);

const sourceContainerClient = blobServiceClient.getContainerClient(SOURCE_CONTAINER_NAME);
const sourceBlockBlobClient = sourceContainerClient.getBlockBlobClient(filename);

const destinationContainerClient = blobServiceClient.getContainerClient(DESTINATION_CONTAINER_NAME);
const destinationBlockBlobClient = finalContainerClient.getBlockBlobClient(filename);

const sourceFileBuffer = await sourceBlockBlobClient.downloadToBuffer();
destinationBlockBlobClient.uploadData(sourceFileBuffer);

字符串

ygya80vv

ygya80vv1#

在Azure Blob容器之间复制文件。
您可以使用下面的代码使用JavaScript SDK将文件从一个容器复制到另一个容器。

验证码:

const { BlobServiceClient } = require('@azure/storage-blob');

const CONNECTION_STRING = '<Your-connection -string>';
const SOURCE_CONTAINER_NAME = 'test'; //<your-source-container-name>
const DESTINATION_CONTAINER_NAME = 'test1'; //<Your-destination-container-name>
const filename = 'sample012.txt';

async function copyFile() {
  const blobServiceClient = BlobServiceClient.fromConnectionString(CONNECTION_STRING);

  const sourceContainerClient = blobServiceClient.getContainerClient(SOURCE_CONTAINER_NAME);
  const sourceBlockBlobClient = sourceContainerClient.getBlockBlobClient(filename);

  const destinationContainerClient = blobServiceClient.getContainerClient(DESTINATION_CONTAINER_NAME);
  const destinationBlockBlobClient = destinationContainerClient.getBlockBlobClient(filename);

  const sourceBlobUrl = sourceBlockBlobClient.url;
  await destinationBlockBlobClient.startCopyFromURL(sourceBlobUrl);

  console.log(`File ${filename} copied from ${SOURCE_CONTAINER_NAME} to ${DESTINATION_CONTAINER_NAME}`);
}

copyFile().catch((err) => {
  console.error('Error:', err.message);
})

字符串

输出:

File sample012.txt copied from test to test1


的数据

门户:

参考号:

Get started with Azure Blob Storage and JavaScript - Azure Storage | Microsoft Learn

相关问题