【问题标题】:SyntaxError: Unexpected identifier, blobServiceClient: any;SyntaxError: Unexpected identifier, blobServiceClient: any;
【发布时间】:2021-06-09 10:08:21
【问题描述】:

**我正在创建一个将生成 sasurl 的类,我需要将文件上传到前端。我将在其他模块中使用 BlobStorageApi 类来创建 API。但是当我运行这段代码时,我得到了 SyntaxError: Unexpected identifier。这段代码之前不在课堂上并且正在工作,但是在我把它放在课堂上之后它给了我一个错误。

const storage = require("@azure/storage-blob");

class BlobStorageAPI {
  blobServiceClient: any;
    containerName: string;
    client: any;
    blobName: string;
    blobClient: any;
    creds: any;
  constructor(account, accountKey, accountName) {
    const creds = new storage.StorageSharedKeyCredential(account, accountKey);
    this.blobServiceClient = new storage.BlobServiceClient(
      `https://${accountName}.blob.core.windows.net`,
      creds
    );
    this.containerName = 'text'
    this.client = this.blobServiceClient.getContainerClient(this.containerName)
    this.blobName = 'help.txt'
    this.blobClient = this.client.getBlobClient(this.blobName)
  }

  get blobSAS() {
    return storage
      .generateBlobSASQueryParameters({
        containerName:this.containerName,
        blobName: this.blobName,
          permissions: storage.BlobSASPermissions.parse("racwd"),
          startsOn: new Date(),
          expiresOn: new Date(new Date().valueOf() + 86400),
        },
        this.creds
      ).toString();
  }
  get sasUrl() {
    return this.blobClient.url + "?" + this.blobSAS;
  }
}

const api = new BlobStorageAPI('joey','/Zs0kjnFoKtKqDPdiAV/61+pTdRhr5H5Wd5vQheXoEtuTOjtWLp6w==', "blobstorage0420");
console.log(api.blobSAS);
console.log();
console.log(api.sasUrl);

【问题讨论】:

    标签: javascript typescript class constructor azure-blob-storage


    【解决方案1】:

    试试代码:

    const storage = require("@azure/storage-blob");
    
    class BlobStorageAPI {
    
      constructor(accountName, accountKey) {
        this.creds = new storage.StorageSharedKeyCredential(accountName, accountKey);
        this.blobServiceClient = new storage.BlobServiceClient(
          `https://${accountName}.blob.core.windows.net`,
          this.creds
        );
        this.containerName = 'your-container-name'
        this.client = this.blobServiceClient.getContainerClient(this.containerName)
        this.blobName = 'your-blob-name'
        this.blobClient = this.client.getBlobClient(this.blobName)
      }
    
      get blobSAS() {
        return storage
          .generateBlobSASQueryParameters({
            containerName:this.containerName,
            blobName: this.blobName,
              permissions: storage.BlobSASPermissions.parse("racwd"),
              startsOn: new Date(),
              expiresOn: new Date(new Date().valueOf() + 86400),
            },
            this.creds
          ).toString();
      }
    
      get sasUrl() {
        // console.log(this.blobName, this.containerName)
        return this.blobClient.url + "?" + this.blobSAS;
      }
    }
    
    const account = "storage-account-name";
    const key = "account-key";
    
    const api = new BlobStorageAPI(account, key);
    console.log(api.blobSAS);
    console.log();
    console.log(api.sasUrl);
    

    我该怎么做:

    1. 删除blobServiceClient: any;...
    2. const creds 更改为this.creds
    3. 更改为StorageSharedKeyCredential(accountName, accountKey),删除代码中的account

    结果:

    更多关于 JavaScript 构造函数的详细信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor

    【讨论】:

      猜你喜欢
      • 2019-10-06
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多