Skip to content

Instantly share code, notes, and snippets.

@Miopas
Created December 9, 2020 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Miopas/350f24a10f7a7ccfe69474caf002fddc to your computer and use it in GitHub Desktop.
Save Miopas/350f24a10f7a7ccfe69474caf002fddc to your computer and use it in GitHub Desktop.
Download and upload files for Google Cloud Platform.
'''
To access your bucket in Linux, you need to download the project credential from GCP and set it the environment variable as
> export GOOGLE_APPLICATION_CREDENTIALS='/path/to/dir/*.json'
Same for uploading files.
'''
import sys
from google.cloud import storage
def download_blob(bucket_name, source_blob_name, destination_file_name):
'''Downloads a blob from the bucket.'''
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))
filename = sys.argv[1]
bucket_name = 'mybucket'
download_blob(bucket_name, '{}'.format(filename), 'pretraining_data/{}'.format(filename))
from google.cloud import storage
import sys
def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""
# bucket_name = "your-bucket-name"
# source_file_name = "local/path/to/file"
# destination_blob_name = "storage-object-name"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print(
"File {} uploaded to {}.".format(
source_file_name, destination_blob_name
)
)
source_file_name = sys.argv[1]
bucket_name = 'mybucket'
upload_blob(bucket_name, source_file_name, source_file_name.split('/')[-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment