我正在對 Azure Data Lake Storage 中的容器進行獲取 Blob 串列呼叫,其中包含 Blob 和目錄。
請求如下所示:
{
url: 'https://{account}.blob.core.windows.net/container-1?comp=list&restype=container&prefix=directory-1/&maxresults=100',
method: 'GET',
headers: {
'x-ms-date': 'Tue, 18 Jan 2022 05:58:28 GMT',
'x-ms-version': '2019-02-02',
Authorization: 'Bearer {Token}'
}
}
回應看起來像這樣:
{
"EnumerationResults": {
"Prefix": "directory-1/",
"MaxResults": "100",
"Blobs": {
"Blob": [
{
"Name": "directory-1/directory1-1",
"Properties": {
"Creation-Time": "Wed, 12 Jan 2022 10:56:50 GMT",
"Last-Modified": "Wed, 12 Jan 2022 10:56:50 GMT",
"Etag": "0x8D9D5BA3C8CC53E",
"Content-Length": "0",
"Content-Type": "application/octet-stream",
"Content-Encoding": "",
"Content-Language": "",
"Content-CRC64": "AAAAAAAAAAA=",
"Content-MD5": "",
"Cache-Control": "",
"Content-Disposition": "",
"BlobType": "BlockBlob",
"AccessTier": "Hot",
"AccessTierInferred": "true",
"LeaseStatus": "unlocked",
"LeaseState": "available",
"ServerEncrypted": "true"
}
},
{
"Name": "directory-1/directory1-1/file1-1-1.csv",
"Properties": {
"Creation-Time": "Thu, 13 Jan 2022 13:08:34 GMT",
"Last-Modified": "Thu, 13 Jan 2022 13:08:34 GMT",
"Etag": "0x8D9D695CDD159F1",
"Content-Length": "80205",
"Content-Type": "text/csv",
"Content-Encoding": "",
"Content-Language": "",
"Content-CRC64": "",
"Content-MD5": "D/UezNpgI t6xFpVw3tUGA==",
"Cache-Control": "",
"Content-Disposition": "",
"BlobType": "BlockBlob",
"AccessTier": "Hot",
"AccessTierInferred": "true",
"LeaseStatus": "unlocked",
"LeaseState": "available",
"ServerEncrypted": "true"
}
},
{
"Name": "directory-1/file1-1.csv",
"Properties": {
"Creation-Time": "Wed, 12 Jan 2022 05:45:28 GMT",
"Last-Modified": "Thu, 13 Jan 2022 14:04:43 GMT",
"Etag": "0x8D9D69DA5DA0F28",
"Content-Length": "65",
"Content-Type": "text/csv",
"Content-Encoding": "",
"Content-Language": "",
"Content-CRC64": "",
"Content-MD5": "Xqyu Y7Jhxu2n7INUROqNg==",
"Cache-Control": "",
"Content-Disposition": "",
"BlobType": "BlockBlob",
"AccessTier": "Hot",
"AccessTierInferred": "true",
"LeaseStatus": "unlocked",
"LeaseState": "available",
"ServerEncrypted": "true"
}
},
{
"Name": "directory-1/file1-2.json",
"Properties": {
"Creation-Time": "Wed, 12 Jan 2022 05:45:28 GMT",
"Last-Modified": "Thu, 13 Jan 2022 14:07:17 GMT",
"Etag": "0x8D9D69E01C01B66",
"Content-Length": "414",
"Content-Type": "application/json",
"Content-Encoding": "",
"Content-Language": "",
"Content-CRC64": "",
"Content-MD5": "xxdWz9XwRegDoYI OrG6tg==",
"Cache-Control": "",
"Content-Disposition": "",
"BlobType": "BlockBlob",
"AccessTier": "Hot",
"AccessTierInferred": "true",
"LeaseStatus": "unlocked",
"LeaseState": "available",
"ServerEncrypted": "true"
}
},
{
"Name": "directory-1/file1-3.jpeg",
"Properties": {
"Creation-Time": "Wed, 12 Jan 2022 05:45:28 GMT",
"Last-Modified": "Wed, 12 Jan 2022 05:45:28 GMT",
"Etag": "0x8D9D58EBD29AFA4",
"Content-Length": "172946",
"Content-Type": "image/jpeg",
"Content-Encoding": "",
"Content-Language": "",
"Content-CRC64": "",
"Content-MD5": "JVLMabvgKvlALNE4V/7eaA==",
"Cache-Control": "",
"Content-Disposition": "",
"BlobType": "BlockBlob",
"AccessTier": "Hot",
"AccessTierInferred": "true",
"LeaseStatus": "unlocked",
"LeaseState": "available",
"ServerEncrypted": "true"
}
},
{
"Name": "directory-1/test",
"Properties": {
"Creation-Time": "Thu, 13 Jan 2022 08:56:13 GMT",
"Last-Modified": "Thu, 13 Jan 2022 08:56:13 GMT",
"Etag": "0x8D9D6728D3B1933",
"Content-Length": "0",
"Content-Type": "",
"Content-Encoding": "",
"Content-Language": "",
"Content-CRC64": "",
"Content-MD5": "",
"Cache-Control": "",
"Content-Disposition": "",
"BlobType": "BlockBlob",
"AccessTier": "Hot",
"AccessTierInferred": "true",
"LeaseStatus": "unlocked",
"LeaseState": "available",
"ServerEncrypted": "true"
}
},
{
"Name": "directory-1/test/:file.csv",
"Properties": {
"Creation-Time": "Thu, 13 Jan 2022 08:56:13 GMT",
"Last-Modified": "Thu, 13 Jan 2022 08:56:13 GMT",
"Etag": "0x8D9D6728D3FD74D",
"Content-Length": "14",
"Content-Type": "text/csv",
"Content-Encoding": "",
"Content-Language": "",
"Content-CRC64": "",
"Content-MD5": "0X493GkdoXENg7klv3zR8g==",
"Cache-Control": "",
"Content-Disposition": "",
"BlobType": "BlockBlob",
"AccessTier": "Hot",
"AccessTierInferred": "true",
"LeaseStatus": "unlocked",
"LeaseState": "available",
"ServerEncrypted": "true"
}
},
{
"Name": "directory-1/test/file.csv",
"Properties": {
"Creation-Time": "Thu, 13 Jan 2022 08:59:10 GMT",
"Last-Modified": "Thu, 13 Jan 2022 08:59:10 GMT",
"Etag": "0x8D9D672F701A8DA",
"Content-Length": "14",
"Content-Type": "text/csv",
"Content-Encoding": "",
"Content-Language": "",
"Content-CRC64": "",
"Content-MD5": "0X493GkdoXENg7klv3zR8g==",
"Cache-Control": "",
"Content-Disposition": "",
"BlobType": "BlockBlob",
"AccessTier": "Hot",
"AccessTierInferred": "true",
"LeaseStatus": "unlocked",
"LeaseState": "available",
"ServerEncrypted": "true"
}
}
]
},
"NextMarker": "",
"_ServiceEndpoint": "https://{account}.blob.core.windows.net/",
"_ContainerName": "container-1"
}
}
這里有些結果是實際的 blob/檔案,而有些是目錄。 如何通過查看回應來區分目錄和檔案?
PS:最初我認為 application/octet-stream 的 content-type 將用于目錄和任何其他格式的 blob/files 但這也不起作用,因為所有 xlsx 檔案也具有 application/octet-stream 以回應 Azure Data Lake Storage。
完整的 NodeJS 代碼是:
const request = require('request')
const account = 'add your account here'
const strTime = new Date().toUTCString()
const containerName = 'container-1'
const BearerToken = 'Add your token here'
const options = {
url: `https://${account}.blob.core.windows.net/${containerName}?comp=list&restype=container&prefix=directory-1/&delimiter=`,
headers: {
Authorization: `Bearer ${BearerToken}`,
'x-ms-date': strTime, // var strTime = new Date().toUTCString();
'x-ms-version': '2019-02-02' // Stable xms version
}
}
function callback (error, response, body) {
if (error) console.log(error)
console.log(body)
}
request(options, callback)
uj5u.com熱心網友回復:
如評論中所述,您需要在delimiter=/請求中添加引數。所以你的請求會是這樣的:
https://{account}.blob.core.windows.net/container-1?comp=list&restype=container&prefix=directory-1/&maxresults=100&delimiter=/
當您請求包含引數時,您將看到回應中元素delimiter下回傳的虛擬檔案夾/目錄。BlobPrefix
請參閱此鏈接以了解有關 delimiter 引數的更多資訊:https ://docs.microsoft.com/en-us/rest/api/storageservices/list-blobs#remarks (“使用分隔符遍歷 Blob 命名空間”部分) .
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/416131.html
標籤:
