无法使用angular-azure-blob-service生成“ sas”令牌

时间:2018-07-04 08:11:50

标签: angular azure blob azure-storage-blobs

 if (fileUpload !== null) {
      const baseUrl = this.blob.generateBlobUrl(Config,'testBlobUpload');
      this.config = {
        baseUrl: baseUrl,
        sasToken: Config.sas,
        blockSize: 1024 * 64, // OPTIONAL, default value is 1024 * 32
        file: fileUpload.nativeFile,
        complete: () => {
          console.log('Transfer completed !');
        },
        error: () => {
          console.log('Error !');
        },
        progress: (percent) => {
          //this.percent = percent;
        }
      };
      this.blob.upload(this.config);
    }
    
    
    export const Config: UploadParams = {
        sas:'?sv=2017-07-29&sr=b&sig=q80fVo0wp8SVmTcgae%2BjceUPfKjE3Eb2MYbkClm8EqQ%3D&st=2018-03-06T13%3A49%3A33Z&se=2018-11-01T13%3A49%3A33Z&sp=racw',
        storageAccount: 'vldevstoragefuncapp.blob.core.windows.net/vl2songs',
        containerName: 'vl2songs'
        //?sv=2017-07-29&sr=b&sig=QXVl6BcV47WiOPbM8CeNhmGpC%2FNslyN7qnI%2BopLuXyg%3D&st=2018-03-06T11%3A46%3A49Z&se=2018-11-01T11%3A46%3A50Z&sp=rw
      }
      

使用Angular5使用angular-azure-blob-service在Azure blob上上传媒体文件,我面临生成'sas'令牌的问题,如果有人用过,谢谢

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

要在Angular 2+中生成SAS令牌,您需要安装-create-hmac

npm i create-hmac

需要插入到index.html

<head>
  ...

  <script>
    var global = global || window;
    var Buffer = Buffer || [];
    var process = process || {
      env: { DEBUG: undefined },
      version: []
    };
  </script>
</head>

生成令牌-

public generateSasToken(resourceUri: string, signingKey: string, policyName: string, expiresInMins: number) {
    resourceUri = encodeURIComponent(resourceUri);
    let expires = (Date.now() / 1000) + expiresInMins * 60;
    expires = Math.ceil(expires);
    const toSign = resourceUri + '\n' + expires;

    // Use crypto
    const createHmac = require('create-hmac');
    const Buffer = require('buffer').Buffer;
    const hmac = createHmac('sha256', Buffer.from(signingKey, 'base64'));
    hmac.update(toSign);
    const base64UriEncoded = encodeURIComponent(hmac.digest('base64'));

    // Construct authorization string
    let token = 'SharedAccessSignature sr=' + resourceUri + '&sig='
      + base64UriEncoded + '&se=' + expires;
    if (policyName) {
      token += '&skn=' + policyName;
    }
    return token;
}