在Azure Blob容器之间复制文件

huangapple go评论94阅读模式
英文:

Copy File between azure blobs containers

问题

我正在使用@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 = destinationContainerClient.getBlockBlobClient(filename);

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

I'm using the @azure/storage-blob package to manipulate files in Azure.

In the same Azure storage account I have two storage containers i.e. source and destination.

What I'm trying to do is copy a file located in the source container to the destination container without having to download the file.

I'm currently downloading to a buffer and uploading to the destination.

Is there any way to do this directly?

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);

答案1

得分: 0

以下是翻译好的部分:

复制 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(`文件 ${filename} 已从 ${SOURCE_CONTAINER_NAME} 复制到 ${DESTINATION_CONTAINER_NAME}`);
}

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

输出:

文件 sample012.txt 已从 test 复制到 test1

门户:
在Azure Blob容器之间复制文件

参考:

使用 Azure Blob 存储和 JavaScript 入门 - Azure 存储 | Microsoft Learn

英文:

> Copy File between Azure blobs containers.

You can use the below code to copy a file from one container to another container using javascript SDK.

Code:

const { BlobServiceClient } = require(&#39;@azure/storage-blob&#39;);

const CONNECTION_STRING = &#39;&lt;Your-connection -string&gt;&#39;;
const SOURCE_CONTAINER_NAME = &#39;test&#39;; //&lt;your-source-container-name&gt;
const DESTINATION_CONTAINER_NAME = &#39;test1&#39;; //&lt;Your-destination-container-name&gt;
const filename = &#39;sample012.txt&#39;;

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) =&gt; {
  console.error(&#39;Error:&#39;, err.message);
})

Output:

File sample012.txt copied from test to test1

在Azure Blob容器之间复制文件

Portal:
在Azure Blob容器之间复制文件

Reference:

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

答案2

得分: 0

我已解决了以下代码的问题:

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);

await destinationBlockBlobClient.syncCopyFromURL(`${sourceBlockBlobClient.url}?${sasToken}`);

使用syncCopyFromUrl函数,我只需传递一个URL,文件就会被正确且更快地复制。在我的场景中,我需要生成一个sasToken以确保文件可以被复制。

英文:

I`ve solved the problem with the following code:

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);

await destinationBlockBlobClient.syncCopyFromURL(`${sourceBlockBlobClient.url}?${sasToken}`);

Using the syncCopyFromUrl function I did just pass an URL and the file was copied correctly and faster.
In my scenario, I've needed to generate a sasToken to ensure the file was accessible for copying.

huangapple
  • 本文由 发表于 2023年8月4日 23:44:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定