gpt4 book ai didi

node.js - 无法查看上传到 azure blob 存储的图像

转载 作者:行者123 更新时间:2023-12-03 03:13:27 24 4
gpt4 key购买 nike

我正在使用 Node.js 将图像上传到 Azure 存储 https://github.com/Azure/azure-storage-node 。上传成功,但是访问URL看不到图片。

上传代码如下所示。

var file = 'tmp/myimage.png';
var blobService = azure.createBlobService(config.azure.connection_string);

blobService.createBlockBlobFromLocalFile(config.azure.container, 'taskblob', file, function(err, result, response) {
if(err) return console.log(err);
console.log(response);
callback();
});

在 Azure 门户中,我可以看到某些内容已上传到我的容器,访问提供的 URL 仅会加载一个空白页面。

https://<storage>.blob.core.windows.net/<container>/taskblob

在记录“响应”时,我还收到了来自 Azure 的成功响应

最佳答案

@wazzaday,一般来说,我们可以使用您提供的代码将文件上传到 Azure Blob Stroage。

    var azure = require('azure-storage');
var blobSvc = azure.createBlobService("**","**");
var file = 'tmp/1.txt';

blobSvc.createContainerIfNotExists('mycontainer', function (error, result, response) {
if (!error) {
// Container exists and allows
// anonymous read access to blob
// content and metadata within this container
console.log('ok')
}
});
blobSvc.createBlockBlobFromLocalFile('mycontainer', 'myblob1', file, function (error, result, response) {
if (!error) {
console.log('file uploaded'+response)
} else {
console.log(error);
}
});

从上面的代码中,我们需要确保文件路径是正确的。由于您在Azure Portal上的文件大小为0,我建议您可以尝试ReadStream上传您的文件并再次检查文件大小。请引用这段代码:

var azure = require('azure-storage');
var fs = require('fs');
var blobSvc = azure.createBlobService("**","**");
var file = 'tmp/1.txt';
var stream = fs.createReadStream(file)
var dataLength = 0;
// using a readStream that we created already
stream
.on('data', function (chunk) {
dataLength += chunk.length;
})
.on('end', function () { // done
console.log('The length was:', dataLength);
});
blobSvc.createContainerIfNotExists('mycontainer', function (error, result, response) {
if (!error) {
// Container exists and allows
// anonymous read access to blob
// content and metadata within this container
console.log('ok')
}
});



blobSvc.createBlockBlobFromStream('mycontainer', 'filename', stream,dataLength, function (error) {
if (!error) {
console.log('ok Blob uploaded')
}

});

请尝试上面的代码,如有更新,请告诉我。

关于node.js - 无法查看上传到 azure blob 存储的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33526951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com