7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Azure FunctionsでAzure SDK for Node.jsを使い、BlobのSAS付きURLを取得する

Last updated at Posted at 2016-10-24

準備

Kuduを使ってAzure SDK for Node.js をインストールしておく

参考
Azure Functionsで npmモジュールを使う

実装

var azure = require("azure");

//ここを書き換える
var storageAccount = "your-storage-account";
var storageAccessKey = "your-storage-access-key";
var container = "your-container-name";
//ここを書き換える

var blobService = azure.createBlobService(storageAccount, storageAccessKey);


function getSAS(context, blob) {


    var startDate = new Date();
    startDate.setMinutes(startDate.getMinutes() - 5 );
    var expiryDate = new Date(startDate);
    expiryDate.setMinutes(startDate.getMinutes() + 10);


    context.log("blobService: " + JSON.stringify(blobService.host));
    context.log("startDate: " + startDate.toISOString());
    context.log("expiryDate: " + expiryDate.toISOString());


    var sharedAccessPolicy = {
        AccessPolicy: {
            Permissions: azure.Constants.BlobConstants.SharedAccessPermissions.READ,
            Start: startDate.toISOString(),
            Expiry: expiryDate.toISOString()
        }
    };

    context.log(JSON.stringify(sharedAccessPolicy));

    var signature = blobService.generateSharedAccessSignature(container, blob, sharedAccessPolicy);
    console.log("signature: " + signature);

    return signature;

}


module.exports = function(context, req) {
    context.log("Node.js HTTP trigger function processed a request. RequestUri=%s", req.originalUrl);

    var fileKey;

    if (req.query.fileKey|| (req.body && req.body.fileKey)) {
        fileKey = (req.query.fileKey || req.body.fileKey);
    }

    if (!fileKey) {
        context.res = {
            status: 400,
            body:  { result: "ng", error: "Please pass a fileKey on the query string or in the request body" }
        };
        context.done();
        return;
    }


    var sas = getSAS(context, fileKey);
    var url = blobService.getUrl(container,fileKey,sas);
    
    console.log(url);

    var responceBody = {
        url: url
    };
    
    context.res = {
        // status: 200, /* Defaults to 200 */
        body: responceBody
    };

    context.done();

};

上記の例では、現在時刻の5分前 から 10分間有効なSASを発行している。

実行時の要求本文(例)

{
    "fileKey": "sastestdir/sastestfile.txt"
}

アクセスポリシのパーミッションの複数指定

var sharedAccessPolicy = {
        AccessPolicy: {
            Permissions: azure.Constants.BlobConstants.SharedAccessPermissions.READ + azure.Constants.BlobConstants.SharedAccessPermissions.LIST,
            Start: startDate.toISOString(),
            Expiry: expiryDate.toISOString()
        }
    };

.READは単純に文字列で"r"、.LISTは"l"なので
+で繋げて渡してやれば良い。
Permissionsを変更すると、クエリストリングのspが sp=rになったり、sp=rl になったりするので、
urlを見て確認できる。


何か間違ってたり、もっとこうしたほうがいいよなどあれば是非コメントにて指摘、アドバイスください:blush:

7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?