seanulus
seanulus

Reputation: 885

Trouble uploading file to google cloud storage

I am having an issue trying to upload a file to Google Cloud Storage. I am getting the error ENOENT: no such file or directory, stat 'file.extension'. My current implementation looks like this:

const { Storage } = require('@google-cloud/storage');

const storage = new Storage();
const bucketName = 'bucket-name';

exports.uploadFileGCP = async (file, anchorID) => {
    const { filename, mimetype, encoding, createReadStream } = await file
    await storage.bucket(bucketName).upload(filename, {
        destination: `anchor/${anchorID}/` + filename,
        predefinedAcl: 'publicRead',
        metadata: {
            cacheControl: 'no-cache'
        },
    });

    console.log(`${filename} uploaded to ${bucketName}`)
}

Looking through the documentation, it appears that the filename in the upload portion requires a path to the file on the local machine. I can't seem to find a solution in which I can upload the file without using a local path. Is there a way to do that?

This is the code for my GraphQL resolver where the file is coming through:

async function uploadFile(parent, args, ctx, info) {

    const google = await uploadFileGCP(args.file, args.anchorID);
    console.log(google)
    ...
}

I feel like I'm missing something obvious but, can't quite figure out what's going on. Thank you for any help on the matter.

Upvotes: 2

Views: 1890

Answers (1)

Methkal Khalawi
Methkal Khalawi

Reputation: 2477

as @Kolban said you should use createWriteStream :

bucketName.file(filename).createWriteStream({resumable: false, gzip: true})

Upvotes: 1

Related Questions