英文:
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 存储和 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('@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);
})
Output:
File sample012.txt copied from test to test1
Portal:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论