带有NPM天蓝色存储的Azure文件不会返回访问令牌

时间:2019-07-25 14:23:33

标签: node.js azure-storage access-token azure-storage-files azure-sas

NPM 天蓝色存储版本为 2.10.3

以下代码尝试在现有共享,目录和文件上生成SAS令牌。代码运行完毕,但不返回令牌。

我正在使用console.log查看停止的位置。唯一的日志是第一个:即将生成令牌。

我正在建立一个成功添加和删除文件的类-该部分起作用。仅获得SAS令牌并不会做任何事情-它不会失败-它不会成功。

为什么连错误都没有?如何获得令牌?

const azure = require('azure-storage');

try{
  const connectionString = "xxx";
  const share = "yyy";
  const directory = "this-is-a-test";
  const file = "short_en.mp3";

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

  const sharedAccessPolicy =  {
      AccessPolicy: {
        Permissions: azure.FileUtilities.SharedAccessPermissions.READ,
        Start: startDate,
        Expiry: expiryDate
      }
  };            

  const fileService = new azure.FileService(connectionString);

  console.log("about to generate token");

  fileService.generateSharedAccessSignature(share, directory, file, sharedAccessPolicy, (error, result) => {

    console.log("getting token response");

    if(error) console.log(`result ${JSON.stringify(error)}`);

      console.log(`result ${JSON.stringify(result)}`);

  });

}catch(err){
  console.log(`result ${JSON.stringify(err)}`);
}

2 个答案:

答案 0 :(得分:0)

您仅启用了READ,这就是您想要的吗?

let startDate = new Date();
let expiryDate = new Date(startDate);
expiryDate.setFullYear(startDate.getFullYear() + 100);
startDate.setFullYear(startDate.getFullYear());

    let sharedAccessPolicy = {
        AccessPolicy: {
            Permissions: azure.BlobUtilities.SharedAccessPermissions.READ + azure.BlobUtilities.SharedAccessPermissions.WRITE,
            Start: startDate,
            Expiry: expiryDate
        },
    };

您的日期格式正确吗?

还可以查看Azure中的指数重试功能

答案 1 :(得分:0)

问题是我认为API调用是异步的(使用await)。不是-它直接返回一个对象。我还找到了用于格式化开始和到期日期的示例代码。这是正确的代码。

    const azure = require('azure-storage');

try{
  const connectionString = "xxx";
  const share = "yyy";
  const directory = "this-is-a-test";
  const file = "short_en.mp3";

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

  var sharedAccessPolicy = {
    AccessPolicy: {
      Permissions: azure.FileUtilities.SharedAccessPermissions.READ,
      Start: startDate,
      Expiry: expiryDate
    }
  };


  const fileService = new azure.FileService(connectionString);

  console.log("about to generate token");

  const accessToken = fileService.generateSharedAccessSignature(share, directory, file, sharedAccessPolicy);

  console.log(`result ${JSON.stringify(accessToken)}`);

}catch(err){
  console.log(`result ${JSON.stringify(err)}`);
}

accessToken看起来像是为安全起见而缩短的sig值:

"st=2019-07-25T18%3A39%3A41Z&se=2019-07-25T18%3A44%3A41Z&sp=r&sv=2018-03-28&sr=f&sig=C2k...NZgYA%3D"