下载时获取 Azure Block Blob 属性 (BlockBlobClient.download)
Get Azure Block Blob properties while downloading it (BlockBlobClient.download)
我正在尝试使用 SASToken 从 azure blob 存储容器下载 blob url。
代码 v1:
const blobClient = new BlobClient(urlWithSASToken, new AnonymousCredential()).getBlockBlobClient();
blobClient.download().then(async result => {
const blob: Blob = await result.blobBody;
console.log('Azure file metadata:');
console.log(result.metadata);
const fileName: string = result.metadata.filename;
FileHelper.downloadBlobAs(blob, fileName);
});
代码 v2:
try {
const blobClient = new BlobClient(urlWithSASToken, new AnonymousCredential()).getBlockBlobClient();
const downloadResponse = await blobClient.download();
const blob: Blob = await downloadResponse.blobBody;
console.log('Azure file metadata:');
console.log(downloadResponse.metadata);
const fileName: string = downloadResponse.metadata.filename;
FileHelper.downloadBlobAs(blob, fileName);
} catch (err: any) {}
文件已下载,但名称为“undefined”,因为 result.metadata 似乎是空的。我查看了 XHR,那里有我正在寻找的元数据(如“x-ms-meta-filename”),但似乎在 BlobDownloadResponseParsed.metadata 对象中不可用。
CORS 是使用 ARM 模板设置的。这是相关部分:
"cors": {
"corsRules": [{
"allowedOrigins": [
"[parameters('WebsiteURL')]"
],
"allowedMethods": [
"PUT",
"GET"
],
"maxAgeInSeconds": 0,
"exposedHeaders": [
"Access-Control-*"
],
"allowedHeaders": [
"content-type",
"x-ms-blob-content-type",
"x-ms-blob-type",
"x-ms-client-request-id",
"x-ms-meta-filename",
"x-ms-version"
]
}]
}
要检索 Blob 的元数据,您需要明确获取它们。在 JavaScript 中,您可以通过调用 getProperties()
函数来实现。
Returns all user-defined metadata, standard HTTP properties, and system properties for the blob. It does not return the content of the blob.
更多信息:BlobURL class - getProperties(Aborter, IBlobGetPropertiesOptions)
本质上问题出在您的 CORS 设置上(公开 headers)。您只公开了 Access-Control
相关的响应 headers,这就是为什么您没有取回元数据的原因,因为 x-metadata-*
响应 headers 未公开(阻止)。
请更改 Exposed Headers
以允许所有响应 headers,方法如下:
"exposedHeaders": [
"*"
]
您应该会看到填充的元数据。
我正在尝试使用 SASToken 从 azure blob 存储容器下载 blob url。
代码 v1:
const blobClient = new BlobClient(urlWithSASToken, new AnonymousCredential()).getBlockBlobClient();
blobClient.download().then(async result => {
const blob: Blob = await result.blobBody;
console.log('Azure file metadata:');
console.log(result.metadata);
const fileName: string = result.metadata.filename;
FileHelper.downloadBlobAs(blob, fileName);
});
代码 v2:
try {
const blobClient = new BlobClient(urlWithSASToken, new AnonymousCredential()).getBlockBlobClient();
const downloadResponse = await blobClient.download();
const blob: Blob = await downloadResponse.blobBody;
console.log('Azure file metadata:');
console.log(downloadResponse.metadata);
const fileName: string = downloadResponse.metadata.filename;
FileHelper.downloadBlobAs(blob, fileName);
} catch (err: any) {}
文件已下载,但名称为“undefined”,因为 result.metadata 似乎是空的。我查看了 XHR,那里有我正在寻找的元数据(如“x-ms-meta-filename”),但似乎在 BlobDownloadResponseParsed.metadata 对象中不可用。
CORS 是使用 ARM 模板设置的。这是相关部分:
"cors": {
"corsRules": [{
"allowedOrigins": [
"[parameters('WebsiteURL')]"
],
"allowedMethods": [
"PUT",
"GET"
],
"maxAgeInSeconds": 0,
"exposedHeaders": [
"Access-Control-*"
],
"allowedHeaders": [
"content-type",
"x-ms-blob-content-type",
"x-ms-blob-type",
"x-ms-client-request-id",
"x-ms-meta-filename",
"x-ms-version"
]
}]
}
要检索 Blob 的元数据,您需要明确获取它们。在 JavaScript 中,您可以通过调用 getProperties()
函数来实现。
Returns all user-defined metadata, standard HTTP properties, and system properties for the blob. It does not return the content of the blob.
更多信息:BlobURL class - getProperties(Aborter, IBlobGetPropertiesOptions)
本质上问题出在您的 CORS 设置上(公开 headers)。您只公开了 Access-Control
相关的响应 headers,这就是为什么您没有取回元数据的原因,因为 x-metadata-*
响应 headers 未公开(阻止)。
请更改 Exposed Headers
以允许所有响应 headers,方法如下:
"exposedHeaders": [
"*"
]
您应该会看到填充的元数据。