我有一个node.js的应用程序,我想使用Azure Blob存储作为通过Node.js上传文件和下载文件的地方
我已遵照以下指示:https://github.com/Azure-Samples/storage-blob-upload-from-webapp-node/tree/master/
我已经创建了我的Blob存储帐户,但是现在当我运行我的应用程序时,我得到以下错误:
There was an error contacting the blob storage container.
StorageError: The specified container does not exist.
\node_modules\azure-storage\lib\common\services\storageserviceclient.js:1191:23)
node_modules\azure-storage\lib\common\services\storageserviceclient.js:738:50
node_modules\azure-storage\lib\common\services\storageserviceclient.js:311:37)
node_modules\request\request.js:188:22
node_modules\request\request.js:1171:10
根据我的测试,我们可以使用以下代码上传和下载1。安装sdk
my package.json
"dependencies": {
"@azure/abort-controller": "^1.0.1",
"@azure/storage-blob": "^12.0.2",
"fs": "0.0.1-security",
"stream": "0.0.2"
}
const accountname ="blobstorage0516";
const key = "your account key";
const cerds = new storage.StorageSharedKeyCredential(accountname,key);
const containerName="test";
const pipeline = storage.newPipeline(cerds, {
retryOptions: { maxTries: 4 }, // Retry options
userAgentOptions: { userAgentPrefix: "AdvancedSample V1.0.0" }, // Customized telemetry string
keepAliveOptions: {
// Keep alive is enabled by default, disable keep alive by setting false
enable: false
}
});
const blobServiceClient =new storage.BlobServiceClient( `https://${accountname}.blob.core.windows.net`,pipeline)
var containerClient =blobServiceClient.getContainerClient(containerName)
if(!containerClient.exists()){
console.log("the container does not exit")
await containerClient.create()
}
const blobNmae="test.jpg";
const localFilePath="D:\\download\\test.jpg";
const blockBlobClient =containerClient.getBlockBlobClient(blobNmae)
// upload
await blockBlobClient.uploadStream(fs.createReadStream(localFilePath), 4 * 1024 * 1024, 20, {
abortSignal: AbortController.timeout(30 * 60 * 1000)})
// download
const downloadBlockBlobResponse = await blockBlobClient.download(0);
const datastream = new stream.PassThrough();
const readableStream = downloadBlockBlobResponse.readableStreamBody
readableStream.on("data", data => {
datastream.push(data);
});
readableStream.on("end" , () => {
fs.writeFileSync('D:\\test1.jpg', datastream.read())
console.log("download successfully")
datastream.destroy();
});
readableStream.on("error" , (error) => {
datastream.destroy();
throw error;
});
有关更多详细信息,请参阅文档和示例