gpt4 book ai didi

azure - 使用 Azure Function 将文件上传到特定的 Azure 存储容器

转载 作者:行者123 更新时间:2023-12-03 02:00:41 50 4
gpt4 key购买 nike

现在我正在开发一个 API,允许客户将文件上传到自己的容器。我向他们提供容器名称和 SAS token ,然后他们通过 API 输入此信息以上传到他们的容器。以下是我的代码片段,其中“container”是容器名称,“accessKey”是它们提交的 SAS token :

var bodyBuffer = Buffer.from(req.body);
var boundary = multipart.getBoundary(req.headers['content-type']);
var parts = multipart.Parse(bodyBuffer, boundary);

const sasURL = "https://myaccountname.blob.core.windows.net/" + container + "?" + accessKey;
const containerClient = new BlobContainerClient(sasURL);
const blobName = parts[0].filename;
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobReponse = await blockBlobClient.upload(parts[0].data, parts[0].data.length);

Azure 声明 uploadBlobResponse 已声明,但从未读取其值,但我不知道如何解决该问题。

最佳答案

由于身份验证问题/sas url,您可能会收到 500 错误。
我已经提到了这个document用于创建 CORS 规则和 SAS URL。创建 Javascript 项目并安装文档中列出的包。

套餐:

npm install @azure/storage-blob
npm install parcel

index.js 文件:

const { BlobServiceClient } =  require("@azure/storage-blob");
const selectButton = document.getElementById("select-button");
const fileInput = document.getElementById("file-input");
const blobSasUrl = "your SAS URL";
const blobServiceClient = new BlobServiceClient(blobSasUrl);
const containerName = "container name";
const containerClient = blobServiceClient.getContainerClient(containerName);
const uploadFiles = async () => {
try {
const promises = [];
for (const file of fileInput.files) {
const blockBlobClient = containerClient.getBlockBlobClient(file.name);
promises.push(blockBlobClient.uploadData(file));
}
await Promise.all(promises);
alert("Done.");
}
catch (error) {
alert(error.message);
}
}
selectButton.addEventListener("click", () => fileInput.click());
fileInput.addEventListener("change", uploadFiles);

index.html 文件:

<!DOCTYPE  html>
<html>
<body>
<button id="select-button">Select and upload files</button>
<input type="file" id="file-input" multiple style="display: none;" />
<script type="module" src="index.js"></script>
</body>
</html>

package.json:

{
"name": "blob-quickstart-v12",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "parcel ./index.html"
},
"keywords": [],
"author": "",
"license": "ISC",
"browserslist": [
"last 1 Edge version",
"last 1 Chrome version",
"last 1 Firefox version",
"last 1 safari version",
"last 1 webkit version"
],
"dependencies": {
"@azure/storage-blob": "^12.12.0",
"parcel": "^2.8.0"
},
"devDependencies": {
"buffer": "^5.7.1"}
}

SAS URL:
enter image description here
控制台:
enter image description here

我复制了URL并将其粘贴到浏览器中,显示以下页面
enter image description here

上传文件。上传成功后,会显示done
enter image description here

enter image description here

进入存储帐户,查看上传的文件
enter image description here

关于azure - 使用 Azure Function 将文件上传到特定的 Azure 存储容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76166100/

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