Reputation: 10940
I found this documentation and this documentation about uploading files to Google Cloud Storage. I wrote this code:
const {Storage} = require('@google-cloud/storage')();
const projectId = 'myapp-cd94d';
const storage = new Storage({
projectId: projectId,
});
const bucketName = "myapp-cd94d.appspot.com";
const filename = { 'test': 'file'};
var serviceAccount = require("./serviceAccountKey.json")
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: "myapp-cd94d.appspot.com"
});
var bucket = admin.storage().bucket();
await storage.bucket(bucketName).upload(filename, {
gzip: true,
metadata: {
cacheControl: 'no-cache',
},
});
console.log(`${filename} uploaded to ${bucketName}.`);
I tried to deploy the code and got this error message:
await storage.bucket(bucketName).upload(filename, {
^^^^^^^
SyntaxError: Unexpected identifier
Is there something wrong with my first line
const {Storage} = require('@google-cloud/storage')();
Why are there curly brackets around Storage
? Am I supposed to replace {Storage}
with something else?
Upvotes: 0
Views: 312
Reputation: 2232
Why are there curly brackets around Storage? Am I supposed to replace {Storage} with something else?
The curly braces around Storage
indicate a deconstructing assignment. It allows you to extract multiple values from data stored in objects and Arrays. The example import from the documentation you shared (const {Storage} = require('@google-cloud/storage');
) is correct.
The error message SyntaxError: Unexpected identifier
is not saying that it didn't identify storage
, it's saying that it didn't expect storage
to be there. The error is due to await
.
await
can only be used in an async
function. For example:
async function myAsyncFunction() {
let result = await lengthyFunction();
console.log(result);
}
myAsyncFunction();
Try wrapping the bucket upload call inside an async
function to avoid the error when using await
.
Upvotes: 1