[英]Azure blob storage, How do I change the name of the file being downloaded?
I have an app that is used to download files from Azure blob via node.js and I want to change the name of the file while downloading.我有一个应用程序用于通过 node.js 从 Azure blob 下载文件,我想在下载时更改文件的名称。 For instance, if the filename is XYZ_ABC.txt
I want the filename after downloading to be ABC.txt
.例如,如果文件名为XYZ_ABC.txt
我希望下载后的文件ABC.txt
。
const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");
const account = "<account>";
const defaultAzureCredential = new DefaultAzureCredential();
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
defaultAzureCredential
);
const containerName = "<container name>";
const blobName = "<blob name>";
async function main() {
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobClient = containerClient.getBlobClient(blobName);
return await blobClient.download().then(response=>{
return {message:"File Downloaded)", data:response, responseCode:200};
}).catch(err=>{
console.log(`download failed`, err);
return {message:"download failed", data:err, responseCode:400};
});
}catch(ex){
return {message:"Failed to Download", data:ex, responseCode:400};
}
main();
Above is the code.以上是代码。 How do I change the name of the file being downloaded?如何更改正在下载的文件的名称?
Once you have the blob data downloaded, you'll need to invoke the browser download process from the local data.下载 blob 数据后,您需要从本地数据调用浏览器下载过程。 One option to do this is to use a library like Filesaver.js , which allows setting the download file name as a parameter:一种选择是使用像Filesaver.js这样的库,它允许将下载文件名设置为参数:
var FileSaver = require('file-saver');
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "hello world.txt");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.