Azure函数将文件上传到Azure Blob

时间:2018-08-15 05:15:07

标签: azure azure-storage-blobs azure-functions azure-functions-core-tools

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

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: TypeError: stream.pause is not a function

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

1 个答案:

答案 0 :(得分:0)

要使用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

希望这会有所帮助。