Thomas David Kehoe
Thomas David Kehoe

Reputation: 10930

How to set the destination for file.download() in Google Cloud Storage?

The Google Cloud Storage documentation for download() suggests that a destination folder can be specified:

file.download({
  destination: '/Users/me/Desktop/file-backup.txt'
}, function(err) {});

No matter what value I put in my file is always downloaded to Firebase Cloud Storage at the root level. This question says that the path can't have an initial slash but changing the example to

file.download({
  destination: 'Users/me/Desktop/file-backup.txt'
}, function(err) {});

doesn't make a difference.

Changing the destination to

file.download({
      destination: ".child('Test_Folder')",
    })

resulted in an error message:

EROFS: read-only file system, open '.child('Test_Folder')'

What is the correct syntax for a Cloud Storage destination (folder and filename)?

Changing the bucket from myapp.appspot.com to myapp.appspot.com/Test_Folder resulted in an error message:

Cannot parse JSON response

Also, the example path appears to specify a location on a personal computer's hard drive. It seems odd to set up a Cloud Storage folder for Desktop. Does this imply that there's a way to specify a destination somewhere other than Cloud Storage?

Here's my code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

exports.Storage = functions.firestore.document('Storage_Value').onUpdate((change, context) => {

  const {Storage} = require('@google-cloud/storage');
  const storage = new Storage();
  const bucket = storage.bucket('myapp.appspot.com');

  bucket.upload('./hello_world.ogg')
  .then(function(data) {
    const file = data[0];
    file.download({
    destination: 'Test_Folder/hello_dog.ogg',
  })
    .then(function(data) {
      const contents = data[0];
      console.log("File uploaded.");
    })
    .catch(error => {
      console.error(error);
    });
  })
  .catch(error => {
    console.error(error);
  });
  return 0;
});

Upvotes: 0

Views: 2223

Answers (3)

Nematillo Ochilov
Nematillo Ochilov

Reputation: 370

You can download the files from Google Cloud Storage to your computer using the following code or command Install python on your PC Install GCS on your PC pip install google-cloud-storage kesaktopdi.appspot.com Download .json file and save it in /home/login/ folder Change your account https://console.cloud.google.com/apis/credentials/serviceaccountkey?project=kesaktopdi

import os

ACCOUNT_ID='kesaktopdi'

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/home/login/" + ACCOUNT_ID + ".json"
def download_blob(bucket_name, source_blob_name, destination_file_name):
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(source_blob_name)
    blob.download_to_filename(destination_file_name)
    #print('Blob {} downloaded to {}.'.format(source_blob_name,destination_file_name))

download_blob(ACCOUNT_ID +'.appspot.com',       #account link
              'user.txt',                       #file location on the server
              '/home/login/kesaktopdi.txt')     #file storage on a computer

You can also download files from the Google Cloud Storage server to your computer using the following command.

                 file location on the server     file storage on a computer

gsutil -m cp -r gs://kesaktopdi.appspot.com/text.txt /home/login

The program was created by the APIuz team https://t.me/apiuz

Upvotes: 0

Thomas David Kehoe
Thomas David Kehoe

Reputation: 10930

Thanks Doug, the code is working now:

exports.Storage = functions.firestore.document('Storage_Value').onUpdate((change, context) => {

  const {Storage} = require('@google-cloud/storage');
  const storage = new Storage();
  const bucket = storage.bucket('myapp.appspot.com');

  const options = {
    destination: 'Test_Folder/hello_world.dog'
  };

  bucket.upload('hello_world.ogg', options)
  .then(function(data) {
    const file = data[0];
  });

  return 0;
});

The function gets the file hello_world.ogg from the functions folder of my project, then writes it to Test_Folder in my Firebase Cloud Storage, and changes the name of the file to hello_world.dog. I copied the download URL and audio file plays perfectly.

Yesterday I thought it seemed odd that writing a file to Cloud Storage was called download(), when upload() made more sense. :-)

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317497

According to the documentation:

The only writeable part of the filesystem is the /tmp directory, which you can use to store temporary files in a function instance. This is a local disk mount point known as a "tmpfs" volume in which data written to the volume is stored in memory. Note that it will consume memory resources provisioned for the function.

The rest of the file system is read-only and accessible to the function.

You should use os.tmpdir() to get the best writable directory for the current runtime.

Upvotes: 5

Related Questions