覆盖Azure存储中的现有Blob不适用于NODE [英] Overwrite an existing blob in Azure storage does not work with NODE
问题描述
当我覆盖一个blob,然后更新浏览器仍然缓存
的主要图像,而不是新的一个。我读过,有
缓存控制属性,但我不能实现它。我需要清理刚刚上传的
blob缓存
老问题
我试图用connect-busboy中间件和下面的方法覆盖现有的blob,但是文件没有被覆盖,我不明白为什么。
< blockquote>
createBlockBlobFromStream(container,blob,(Stream),streamLength,
options,callback)→{SpeedSummary}
从一个流中上传一个块blob。如果blob已经存在于
服务上,它将被覆盖。
app.post('/ upload',function(req,res,params){
var name;
req.busboy.on('field',function(fieldname,val){
name = val +'。jpg';
});
req.busboy.on('file',function(fieldname,file,filename,encoding,mimetype){
file.on('data',function(data){
console .log(name);
console.log(data);
var bufferStream = new stream.PassThrough();
bufferStream.end(data);
var blobSvc = azure .createBlobService(accountName,accountKey);
blobSvc.createBlockBlobFromStream('images',name,bufferStream,data.length,function(error,result,response){$ b $ if(!error){
res.send(200,'upload succeeded')
} else {
res.send(500,JSON.stringify(error))
}
})
});
}) ;
});
Per >
createBlockBlobFromStream(容器,blob,(Stream),streamLength,
选项,回调)→{SpeedSummary}
从流中上传块blob。 如果blob已经存在于
服务中,它将被覆盖。为避免覆盖,如果blob存在,
会抛出一个错误,请在options对象中传入一个accessConditions
参数。
因此,您正在使用Azure Storage SDK for Node覆盖现有的Blob文件。
$ b
编辑:
似乎您遇到了上传问题。我建议你尝试使用 createWriteStreamToBlockBlob 而不是 createBlockBlobFromStream
将blob上传到Azure存储。
以下示例使用 connect-busboy 中间件进行工作。创建/公用文件夹。
使用文件夹结构:
@index.js
\public\index .html
INDEX.JS
var express = require('express')
var app = express()
var busboy = require('connect-busboy')
var azure = require('azure-storage')
var accountName =< acountName>
var accountKey =< accountKey>
$ b app.use(busboy())
app.use(express.static('public'))
app.get('/',function (req,res){
res.sendFile('index.html')
})
app.post('/ upload',function(req,res,params ){
req.pipe(req.busboy);
req.busboy.on('file',function(fieldname,file,filename){
var blobSvc = azure .createBlobService(accountName,accountKey)
file.pipe(blobSvc.createWriteStreamToBlockBlob('mycontainer',filename,function(error){$ b $ if if(!error){
res.send(200, '上传成功')
} else {
res.send(500,JSON.stringify(error))
}
}))
} )
$)
app.listen(process.env.PORT || 3000,function(){
console.log('在端口3000上监听的示例应用程序!')
})
INDEX.HTML
<!DOCTYPE html>
< html>
< head>
< title>示例< / title>
< / head>
< body>
< form action =/ uploadmethod =postenctype =multipart / form-data>
选择要上传的图片:
< input type =filename =fileToUploadid =fileToUpload>
< input type =submitvalue =Upload Imagename =submit>
< / form>
< / body>
< / html>
New question
When I overwrite a blob and then update the browser is still caching the main image and not the new one. I have read that there is a cache-control property but I can not implement it. I need to clean the blob cache that has just been uploaded
Old question
I am trying to overwrite an existing blob using connect-busboy middleware and the following method, but the file is not overwritten and I do not understand why.
createBlockBlobFromStream(container, blob, (Stream), streamLength,
options, callback) → {SpeedSummary}Uploads a block blob from a stream. If the blob already exists on the service, it will be overwritten.
app.post('/upload', function(req, res, params) {
var name;
req.busboy.on('field', function (fieldname, val) {
name = val+'.jpg';
});
req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
file.on('data', function (data) {
console.log(name);
console.log(data);
var bufferStream = new stream.PassThrough();
bufferStream.end(data);
var blobSvc = azure.createBlobService(accountName, accountKey);
blobSvc.createBlockBlobFromStream('images', name, bufferStream, data.length, function (error, result, response){
if (!error) {
res.send(200,'upload succeeded')
} else {
res.send(500,JSON.stringify(error))
}
})
});
});
});
Per Azure Storage SDK for Node API Reference,
createBlockBlobFromStream(container, blob, (Stream), streamLength, options, callback) → {SpeedSummary}
Uploads a block blob from a stream. If the blob already exists on the service, it will be overwritten. To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object.
So you are doing right to overwrite the existing blob file with Azure Storage SDK for Node.
Edit:
Seems you have encountered upload issue. I would recommend you to try using createWriteStreamToBlockBlob instead of createBlockBlobFromStream
to upload blob to Azure storage.
The following example works by using connect-busboy middleware. create /public folders.
Use the folder structure:
\index.js
\public\index.html
INDEX.JS
var express = require('express')
var app = express()
var busboy = require('connect-busboy')
var azure = require('azure-storage')
var accountName = "<acountName>"
var accountKey = "<accountKey>"
app.use(busboy())
app.use(express.static('public'))
app.get('/', function(req, res) {
res.sendFile('index.html')
})
app.post('/upload', function(req, res, params) {
req.pipe(req.busboy);
req.busboy.on('file', function(fieldname, file, filename) {
var blobSvc = azure.createBlobService(accountName, accountKey)
file.pipe(blobSvc.createWriteStreamToBlockBlob('mycontainer', filename, function(error) {
if (!error) {
res.send(200, 'upload succeeded')
} else {
res.send(500, JSON.stringify(error))
}
}))
})
})
app.listen(process.env.PORT || 3000, function() {
console.log('Example app listening on port 3000!')
})
INDEX.HTML
<!DOCTYPE html>
<html>
<head>
<title>Sample</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
这篇关于覆盖Azure存储中的现有Blob不适用于NODE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!