SyntaxError: Unexpected identifier, blobServiceClient: any;
Posted
技术标签:
【中文标题】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);
【问题讨论】:
【参考方案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);
我该怎么做:
-
删除
blobServiceClient: any;...
将const creds
更改为this.creds
更改为StorageSharedKeyCredential(accountName, accountKey)
,删除代码中的account
。
结果:
更多关于 javascript 构造函数的详细信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor
【讨论】:
以上是关于SyntaxError: Unexpected identifier, blobServiceClient: any;的主要内容,如果未能解决你的问题,请参考以下文章
Import Unexpected identifier + SyntaxError: Unexpected string
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 from SpringBoot API
SyntaxError: unexpected EOF while parsing
获取 MongoDB 错误 - SyntaxError: Unexpected token?