gpt4 book ai didi

Azure 函数将文件上传到 Azure Blob

转载 作者:行者123 更新时间:2023-12-02 07:26:40 29 4
gpt4 key购买 nike

我尝试使用Azure功能将文件上传到Azure Blob。从我的 Angular 前端,我使用输入上传文件以获取 HTML 5 文件对象

let formData = new FormData();
formData.append('upload', file);

const headers = new HttpHeaders({ 'enctype': 'multipart/form-data' });

const req = new HttpRequest('PUT', url-to-my-azure-function, formData, { headers: headers});
return this.http.request(req);

当Azure功能被触发时,我尝试使用

blobService.createBlockBlobFromStream("mycontainer", "myfilename", req.body, req.body.length, function(error, result) {...});

创建 blob,但它给了我异常(exception)

Exception: TypeError: stream.pause is not a function

当我直接使用输出绑定(bind)来创建 blob 时,它正在工作。知道我做错了什么吗?

最佳答案

在HTML页面中使用input标签上传文件,这里有一个演示供您引用:

var blobUri = 'https://' + 'STORAGE_ACCOUNT' + '.blob.core.windows.net';
var blobService = AzureStorage.Blob.createBlobServiceWithSas(blobUri, 'SAS_TOKEN');

// If one file has been selected in the HTML file input element
var file = document.getElementById('fileinput').files[0];

var customBlockSize = file.size > 1024 * 1024 * 32 ? 1024 * 1024 * 4 : 1024 * 512;
blobService.singleBlobPutThresholdInBytes = customBlockSize;

var finishedOrError = false;
var speedSummary = blobService.createBlockBlobFromBrowserFile('containerName',
file.name, file, {blockSize : customBlockSize}, function(error, result, response) {
finishedOrError = true;
if (error) {
// Upload blob failed
} else {
// Upload successfully
}
});

更多信息可以引用:Azure Storage JavaScript Client Library Sample for Blob Operations

希望这对您有所帮助。

关于Azure 函数将文件上传到 Azure Blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51853307/

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