我有一个存储后端,它使用微软官方的blob存储库(@azure/ NodeJS - Blob )来管理我的Blob存储:
https://www.npmjs.com/package/@azure/storage-blob
有必要将blob从一个文件夹移动到另一个文件夹。不幸的是,我找不到任何关于这方面的文档。
到目前为止,我所做的是:
const { BlobServiceClient } = require("@azure/storage-blob");
const blobServiceClient = BlobServiceClient.fromConnectionString(process.env.storageconnection);
const containerClient = blobServiceClient.getContainerClient('import');
const blobClient = containerClient.getBlobClient('toImport/' + req.body.file);
const downloadBlockBlobResponse = await blobClient.download();
... do some stuff with the value of the files正如您在代码中看到的,我从文件夹"toImport“中读取了一个文件。之后,我想移动文件到另一个文件夹“完成”。这有可能吗?也许我需要创建该文件的副本并删除旧文件?
发布于 2021-08-03 14:48:31
因此,Azure Blob存储中不支持移动操作。您需要做的是将blob从源复制到目标,监视复制进度(因为复制操作是异步的),并在复制完成后删除blob。
对于复制,您希望使用的方法是beginCopyFromURL(string, BlobBeginCopyFromURLOptions)。
请看下面的代码:
const { BlobServiceClient } = require("@azure/storage-blob");
const connectionString = "DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key;EndpointSuffix=core.windows.net";
const container = "container-name";
const sourceFolder = "source";
const targetFolder = "target";
const blobName = "blob.png";
async function moveBlob() {
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
const containerClient = blobServiceClient.getContainerClient(container);
const sourceBlobClient = containerClient.getBlobClient(`${sourceFolder}/${blobName}`);
const targetBlobClient = containerClient.getBlobClient(`${targetFolder}/${blobName}`);
console.log('Copying source blob to target blob...');
const copyResult = await targetBlobClient.beginCopyFromURL(sourceBlobClient.url);
console.log('Blob copy operation started successfully...');
console.log(copyResult);
do {
console.log('Checking copy status...')
const blobCopiedSuccessfully = await checkIfBlobCopiedSuccessfully(targetBlobClient);
if (blobCopiedSuccessfully) {
break;
}
} while (true);
console.log('Now deleting source blob...');
await sourceBlobClient.delete();
console.log('Source blob deleted successfully....');
console.log('Move operation complete.');
}
async function checkIfBlobCopiedSuccessfully(targetBlobClient) {
const blobPropertiesResult = await targetBlobClient.getProperties();
const copyStatus = blobPropertiesResult.copyStatus;
return copyStatus === 'success';
}
moveBlob();https://stackoverflow.com/questions/68637826
复制相似问题