Skip to content

Instantly share code, notes, and snippets.

@SyzBeats
Last active August 29, 2021 21:39
Show Gist options
  • Select an option

  • Save SyzBeats/5c0504299d2333ab1152bd094ba650af to your computer and use it in GitHub Desktop.

Select an option

Save SyzBeats/5c0504299d2333ab1152bd094ba650af to your computer and use it in GitHub Desktop.
Google cloud storage gist
/**
* @file this script provides functions
* that upload and then download a file
* via google cloud storage - a .env file with a key is required for this to work!
*
* @version 0.1
*/
require('dotenv').config();
const path = require("path");
// constants
const BUCKET_NAME = "SOMEBUCKET";
const FILE_PATH = path.join(__dirname, "files/test.txt");
const DEST_FILE_NAME = "test.txt";
const DOWNLOAD_FILE_NAME = "test.txt";
// storage initialization
const {Storage} = require("@google-cloud/storage");
const storage = new Storage();
// simple upload function with destination name
async function uploadFile(){
await storage.bucket(BUCKET_NAME).upload(FILE_PATH, {
destination: DEST_FILE_NAME
});
}
// simple file download with destination name
async function downloadFile(){
await storage.bucket(BUCKET_NAME).file(DEST_FILE_NAME).download({
destination: DOWNLOAD_FILE_NAME
});
}
(async function(){
try {
await downloadFile();
} catch(error){
console.log(error);
return error;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment