问题
I am using Node.js to upload an image to azure storage https://github.com/Azure/azure-storage-node. The upload is successful, but I cannot see the image when I visit the URL.
The upload code looks like.
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();
});
In azure portal I can see something has been uploaded to my container, visiting the provided URL just loads a blank page.
https://<storage>.blob.core.windows.net/<container>/taskblob
I am also getting a success response back from Azure when logging 'response'
回答1:
@wazzaday, Generally, we can upload the files into Azure Blob Stroage using the code as you provided.
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);
}
});
From above code, we need make sure the file path is right. Because your file size is 0 on Azure Portal, I suggest you can try ReadStream to upload your file and check the file size again. Please refer to this code :
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')
}
});
Please try above code, any update, please let me know.
来源:https://stackoverflow.com/questions/33526951/cannot-view-image-upload-to-azure-blob-storage