1- """Programatically create a BigQuery table from a CSV ."""
1+ """Programatically interact with a Google Cloud Storage bucket ."""
22from os import listdir
33from os .path import isfile , join
44from random import randint
55from google .cloud import storage
6- from config import bucketURI , bucketName , localFolder , bucketFolder
6+ from config import bucketName , localFolder , bucketFolder
7+
8+ storage_client = storage .Client ()
9+ bucket = storage_client .get_bucket (bucketName )
710
811
912def upload_files (bucketName ):
10- """Upload files to bucket."""
13+ """Upload files to GCP bucket."""
1114 files = [f for f in listdir (localFolder ) if isfile (join (localFolder , f ))]
12- print (files )
13- storage_client = storage .Client ()
14- bucket = storage_client .get_bucket (bucketName )
1515 for file in files :
1616 localFile = localFolder + file
1717 blob = bucket .blob (bucketFolder + file )
@@ -20,49 +20,41 @@ def upload_files(bucketName):
2020
2121
2222def list_files (bucketName ):
23- """List all files in bucket."""
24- storage_client = storage .Client ()
25- bucket = storage_client .get_bucket (bucketName )
23+ """List all files in GCP bucket."""
2624 files = bucket .list_blobs (prefix = bucketFolder )
2725 fileList = [file .name for file in files if '.' in file .name ]
28- print ('fileList:' , fileList )
2926 return fileList
3027
3128
32- def download_random_file (bucketURI , bucketName , bucketFolder ):
33- """Get random file from bucket."""
34- storage_client = storage .Client ()
35- bucket = storage_client .get_bucket (bucketName )
29+ def download_random_file (bucketName , bucketFolder , localFolder ):
30+ """Download random file from GCP bucket."""
3631 fileList = list_files (bucketName )
3732 rand = randint (0 , len (fileList ) - 1 )
3833 blob = bucket .blob (fileList [rand ])
3934 fileName = blob .name .split ('/' )[- 1 ]
40- blob .download_to_filename ('./files/' + fileName )
41- return fileName
35+ blob .download_to_filename (localFolder + fileName )
36+ return f' { fileName } downloaded from bucket.'
4237
4338
4439def delete_file (bucketName , bucketFolder , fileName ):
45- """Delete file from bucket."""
46- storage_client = storage .Client ()
47- bucket = storage_client .get_bucket (bucketName )
40+ """Delete file from GCP bucket."""
4841 bucket .delete_blob (bucketFolder + fileName )
4942 return f'{ fileName } deleted from bucket.'
5043
5144
5245def rename_file (bucketName , bucketFolder , fileName , newFileName ):
53- """Delete file from bucket."""
54- storage_client = storage .Client ()
55- bucket = storage_client .get_bucket (bucketName )
56- bucket .rename_blob (bucketFolder + fileName ,
46+ """Rename file in GCP bucket."""
47+ blob = bucket .blob (bucketFolder + fileName )
48+ bucket .rename_blob (blob ,
5749 new_name = newFileName )
5850 return f'{ fileName } renamed to { newFileName } .'
5951
6052
6153print (upload_files (bucketName ))
6254print (list_files (bucketName ))
63- print (download_random_file (bucketURI ,
64- bucketName ,
65- bucketFolder ))
55+ print (download_random_file (bucketName ,
56+ bucketFolder ,
57+ localFolder ))
6658print (rename_file (bucketName ,
6759 bucketFolder ,
6860 'test.csv' ,
0 commit comments