gpt4 book ai didi

node.js - 覆盖 Azure 存储中的现有 blob 不适用于 NODE

转载 作者:太空宇宙 更新时间:2023-11-04 02:12:58 25 4
gpt4 key购买 nike

新问题

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

老问题

我尝试使用 connect-busboy 中间件和以下方法覆盖现有的 blob,但该文件没有被覆盖,我不明白为什么。

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))
}
})
});
});
});

最佳答案

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.

因此,您使用 Azure Storage SDK for Node 覆盖现有的 blob 文件是正确的。

编辑:

您似乎遇到了上传问题。我建议您尝试使用 createWriteStreamToBlockBlob而不是 createBlockBlobFromStream 将 Blob 上传到 Azure 存储。以下示例使用 connect-busboy 中间件进行工作。创建/public 文件夹。使用文件夹结构:

\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>

关于node.js - 覆盖 Azure 存储中的现有 blob 不适用于 NODE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41383989/

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