【Microsoft Azure】Blob StorageにアップロードしたコンテンツのURLを取得する (JavaScript)

こんにちは。この記事では、Azure Blob Storageに画像などのコンテンツをアップロードした後に、そのBlobのURLを取得する方法についてまとめておこうと思います。

なお、言語はJavaScript (Node.js)です。

それでは早速参ります。

BlobのURLを取得する方法

結論、blockBlobClientのgetPropertiesメソッドから取得できました!

https://docs.microsoft.com/ja-jp/javascript/api/@azure/storage-blob/blockblobclient?view=azure-node-latest#getProperties_BlobGetPropertiesOptions_

<コード>

こんな感じで、コンテンツをアップロードしたのち、blockBlobClientオブジェクトが持つgetProperties()メソッドを呼ぶことでBlobの様々な情報が取得できます。

const blobServiceClient = BlobServiceClient.fromConnectionString(process.env["CONNSTR"]);
const containerClient = blobServiceClient.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobResponse = await blockBlobClient.upload(parts[0].data, parts[0].data.length);

context.log("Blob was uploaded successfully. requestId: ", uploadBlobResponse.requestId);

const blobUrl = await blockBlobClient.getProperties();
context.log(blobUrl)

ちなみに、getPropertiesで取得できた情報のサンプルを以下に載せておきます。めっちゃたくさんの情報を取得できるんですね・・・

欲しいURL情報は、”url”プロパティにありました。

{
"lastModified": "2021-10-02T03:47:20.000Z",
"createdOn": "2021-10-02T03:47:20.000Z",
"metadata": {},
"objectReplicationRules": {},
"blobType": "BlockBlob",
"leaseState": "available",
"leaseStatus": "unlocked",
"contentLength": 107467,
"contentType": "application/octet-stream",
"etag": "\"0x8D985575650767C\"",
"contentMD5": {
"type": "Buffer",
"data": [
162,
194,
182,
96,
39,
107,
211,
151,
91,
80,
18,
31,
92,
72,
21,
96
]
},
"clientRequestId": "b5ae52ad-3a21-4aee-a827-b0f7c5c636bd",
"requestId": "f16941ac-d01e-0015-1440-b70562000000",
"version": "2020-08-04",
"date": "2021-10-02T03:47:19.000Z",
"acceptRanges": "bytes",
"isServerEncrypted": true,
"accessTier": "Hot",
"accessTierInferred": true,
"versionId": "2021-10-02T03:47:20.6418600Z",
"isCurrentVersion": true,
"body": true,
"_response": {
"headers": {
"_headersMap": {
"accept-ranges": {
"name": "accept-ranges",
"value": "bytes"
},
"content-length": {
"name": "content-length",
"value": "107467"
},
"content-md5": {
"name": "content-md5",
"value": "osK2YCdr05dbUBIfXEgVYA=="
},
"content-type": {
"name": "content-type",
"value": "application/octet-stream"
},
"date": {
"name": "date",
"value": "Sat, 02 Oct 2021 03:47:19 GMT"
},
"etag": {
"name": "etag",
"value": "\"0x8D985575650767C\""
},
"last-modified": {
"name": "last-modified",
"value": "Sat, 02 Oct 2021 03:47:20 GMT"
},
"server": {
"name": "server",
"value": "Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0"
},
"x-ms-access-tier": {
"name": "x-ms-access-tier",
"value": "Hot"
},
"x-ms-access-tier-inferred": {
"name": "x-ms-access-tier-inferred",
"value": "true"
},
"x-ms-blob-type": {
"name": "x-ms-blob-type",
"value": "BlockBlob"
},
"x-ms-client-request-id": {
"name": "x-ms-client-request-id",
"value": "b5ae52ad-3a21-4aee-a827-b0f7c5c636bd"
},
"x-ms-creation-time": {
"name": "x-ms-creation-time",
"value": "Sat, 02 Oct 2021 03:47:20 GMT"
},
"x-ms-is-current-version": {
"name": "x-ms-is-current-version",
"value": "true"
},
"x-ms-lease-state": {
"name": "x-ms-lease-state",
"value": "available"
},
"x-ms-lease-status": {
"name": "x-ms-lease-status",
"value": "unlocked"
},
"x-ms-request-id": {
"name": "x-ms-request-id",
"value": "f16941ac-d01e-0015-1440-b70562000000"
},
"x-ms-server-encrypted": {
"name": "x-ms-server-encrypted",
"value": "true"
},
"x-ms-version": {
"name": "x-ms-version",
"value": "2020-08-04"
},
"x-ms-version-id": {
"name": "x-ms-version-id",
"value": "2021-10-02T03:47:20.6418600Z"
}
}
},
"request": {
"streamResponseStatusCodes": {},
      "url": "https://<blobstoragename>.blob.core.windows.net/<containername>/<filename>",
"method": "HEAD",
"headers": {
"_headersMap": {
"x-ms-version": {
"name": "x-ms-version",
"value": "2020-08-04"
},
"accept": {
"name": "accept",
"value": "application/xml"
},
"x-ms-encryption-algorithm": {
"name": "x-ms-encryption-algorithm",
"value": "AES256"
},
"user-agent": {
"name": "user-agent",
"value": "azsdk-js-storageblob/12.6.0 (NODE-VERSION v14.16.0; Windows_NT 10.0.14393)"
},
"x-ms-client-request-id": {
"name": "x-ms-client-request-id",
"value": "b5ae52ad-3a21-4aee-a827-b0f7c5c636bd"
},
"x-ms-date": {
"name": "x-ms-date",
"value": "Sat, 02 Oct 2021 03:47:20 GMT"
},
"authorization": {
"name": "Authorization",
"value": "xxx"
},
"cookie": {
"name": "Cookie",
"value": ""
}
}
},
"withCredentials": false,
"timeout": 0,
"keepAlive": true,
"decompressResponse": false,
"requestId": "b5ae52ad-3a21-4aee-a827-b0f7c5c636bd",
"operationSpec": {
"path": "/{containerName}/{blob}",
"httpMethod": "HEAD",
"responses": {
"200": {
"headersMapper": {
"serializedName": "Blob_getPropertiesHeaders",
"type": {
"name": "Composite",
"className": "BlobGetPropertiesHeaders",
"modelProperties": {
"lastModified": {
"serializedName": "last-modified",
"xmlName": "last-modified",
"type": {
"name": "DateTimeRfc1123"
}
},
"createdOn": {
"serializedName": "x-ms-creation-time",
"xmlName": "x-ms-creation-time",
"type": {
"name": "DateTimeRfc1123"
}
},
"metadata": {
"serializedName": "x-ms-meta",
"xmlName": "x-ms-meta",
"type": {
"name": "Dictionary",
"value": {
"type": {
"name": "String"
}
}
},
"headerCollectionPrefix": "x-ms-meta-"
},
"objectReplicationPolicyId": {
"serializedName": "x-ms-or-policy-id",
"xmlName": "x-ms-or-policy-id",
"type": {
"name": "String"
}
},
"objectReplicationRules": {
"serializedName": "x-ms-or",
"xmlName": "x-ms-or",
"type": {
"name": "Dictionary",
"value": {
"type": {
"name": "String"
}
}
},
"headerCollectionPrefix": "x-ms-or-"
},
"blobType": {
"serializedName": "x-ms-blob-type",
"xmlName": "x-ms-blob-type",
"type": {
"name": "Enum",
"allowedValues": [
"BlockBlob",
"PageBlob",
"AppendBlob"
]
}
},
"copyCompletedOn": {
"serializedName": "x-ms-copy-completion-time",
"xmlName": "x-ms-copy-completion-time",
"type": {
"name": "DateTimeRfc1123"
}
},
"copyStatusDescription": {
"serializedName": "x-ms-copy-status-description",
"xmlName": "x-ms-copy-status-description",
"type": {
"name": "String"
}
},
"copyId": {
"serializedName": "x-ms-copy-id",
"xmlName": "x-ms-copy-id",
"type": {
"name": "String"
}
},
"copyProgress": {
"serializedName": "x-ms-copy-progress",
"xmlName": "x-ms-copy-progress",
"type": {
"name": "String"
}
},
"copySource": {
"serializedName": "x-ms-copy-source",
"xmlName": "x-ms-copy-source",
"type": {
"name": "String"
}
},
"copyStatus": {
"serializedName": "x-ms-copy-status",
"xmlName": "x-ms-copy-status",
"type": {
"name": "Enum",
"allowedValues": [
"pending",
"success",
"aborted",
"failed"
]
}
},
"isIncrementalCopy": {
"serializedName": "x-ms-incremental-copy",
"xmlName": "x-ms-incremental-copy",
"type": {
"name": "Boolean"
}
},
"destinationSnapshot": {
"serializedName": "x-ms-copy-destination-snapshot",
"xmlName": "x-ms-copy-destination-snapshot",
"type": {
"name": "String"
}
...(truncated to the first 10000 characters)

なお、よりセキュアにするために、SAS URLを取得したい場合もあるかと思います。今回試していませんが、同様にblockBlobClientのgenerateSasUrlメソッドが活用できそうです。

https://docs.microsoft.com/ja-jp/javascript/api/@azure/storage-blob/blockblobclient?view=azure-node-latest#generateSasUrl_BlobGenerateSasUrlOptions_

短いですが、以上、BlobのURLを取得する方法でした!この記事が少しでもお役に立ちましたら、下のいいねボタンをポチっていただけますと励みになります!

おしまい

この記事を気に入っていただけたらシェアをお願いします!

コメントを残す

メールアドレスが公開されることはありません。

ABOUT US

Yuu113
初めまして。Yuu113と申します。 兵庫県出身、東京でシステムエンジニアをしております。現在は主にデータ分析、機械学習を活用してビジネスモデリングに取り組んでいます。日々学んだことや経験したことを整理していきたいと思い、ブログを始めました。旅行、カメラ、IT技術、江戸文化が大好きですので、これらについても記事にしていきたいと思っています。