简体   繁体   English

Azure blob存储,如何更改正在下载的文件的名称?

[英]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.

相关问题 如何在Azure中设置逻辑应用,根据名称将文件从blob存储传输到sharepoint? - How do I set a logic app in Azure to transfer files from blob storage to sharepoint based on the name? 如何在 Azure Blob 存储中覆盖后命名 csv 文件 - How to name a csv file after overwriting in Azure Blob Storage 如何创建连接到我的 Blob 存储的 Azure API? - How do I create an Azure API that connects to my Blob Storage? 如何获取触发 Azure Function Blob 触发器的文件名 - How do I get the file name that triggered a Azure Function Blob Trigger 我可以更改 azure blob 存储的版本计数限制吗? - Can i change count limit of versions for azure blob storage? Azure Blob 存储文件访问 - Azure Blob Storage file access azure blob 存储中的 XLSX 文件被下载为 zip 文件 - XLSX files in azure blob storage get downloaded as zip files SendGrid:如何从 Azure blob 存储附加文件? - SendGrid: How to attach a file from Azure blob storage? 如何在不使用 AZCopy 的情况下将文件系统同步到 Azure Blob 存储? - How to synchronize file systems to Azure Blob storage without using AZCopy? 如何更新 Azure 中放置在 Blob 存储中的 CSV 文件 - How to update CSV file placed at Blob Storage in Azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM