from google.cloud import storage def upload_blob(bucket_name, source_file_name, destination_blob_name): """Uploads a file to the bucket.""" storage_client = storage.Client() bucket = storage_client.get_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)) def copy_blob(bucket_name, blob_name, new_bucket_name, new_blob_name): """Copies a blob from one bucket to another with a new name.""" storage_client = storage.Client() source_bucket = storage_client.get_bucket(bucket_name) source_blob = source_bucket.blob(blob_name) destination_bucket = storage_client.get_bucket(new_bucket_name) new_blob = source_bucket.copy_blob( source_blob, destination_bucket, new_blob_name) print('Blob {} in bucket {} copied to blob {} in bucket {}.'.format( source_blob.name, source_bucket.name, new_blob.name, destination_bucket.name)) def delete_blob(bucket_name, blob_name): """Deletes a blob from the bucket.""" storage_client = storage.Client() bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(blob_name) blob.delete() print('Blob {} deleted.'.format(blob_name)) def is_blob_exists(bucket_name, blob_name): storage_client = storage.Client() bucket = storage_client.get_bucket(bucket_name) blob = bucket.get_blob(blob_name) if blob == None: return False else: return True def list_blobs_with_prefix(BUCKET_NAME, prefix, delimiter=None): storage_client = storage.Client() bucket = storage_client.get_bucket(BUCKET_NAME) blobs = bucket.list_blobs(prefix=prefix, delimiter=delimiter) count = 0 for blob in blobs: count += 1 return count def list_blobs_with_partial_file_name(BUCKET_NAME, prefix, partial_str, delimiter=None): storage_client = storage.Client() bucket = storage_client.get_bucket(BUCKET_NAME) blobs = bucket.list_blobs(prefix=prefix, delimiter=delimiter) count = 0 for blob in blobs: if blob.name.find(partial_str) != -1: print(blob.name) count += 1 return count def get_list_blobs_with_partial_file_name(BUCKET_NAME, prefix, partial_str, delimiter=None): list = [] storage_client = storage.Client() bucket = storage_client.get_bucket(BUCKET_NAME) blobs = bucket.list_blobs(prefix=prefix, delimiter=delimiter) count = 0 for blob in blobs: if blob.name.find(partial_str) != -1: list.append(blob.name) return list #list = get_list_blobs_with_partial_file_name('sme-dsp-raw-demo', 'spotify/archive', 'spotify_streams_v2_sonybmgmusicentertainment_2019-02-21') #print(list)