Skip to content

Commit dc2c7d4

Browse files
committed
rename object bug fix
1 parent 5f63f87 commit dc2c7d4

2 files changed

Lines changed: 20 additions & 28 deletions

File tree

config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
"""BigQuery Upload Configuration."""
1+
"""Google Cloud Storage Configuration."""
22
from os import environ
33

44

55
# Google Cloud Storage
6-
bucketURI = environ.get('GCP_BUCKET_URI')
6+
bucketURL = environ.get('GCP_BUCKET_URL')
77
bucketName = environ.get('GCP_BUCKET_NAME')
88
bucketFolder = environ.get('GCP_BUCKET_FOLDER_NAME')
99

main.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
"""Programatically create a BigQuery table from a CSV."""
1+
"""Programatically interact with a Google Cloud Storage bucket."""
22
from os import listdir
33
from os.path import isfile, join
44
from random import randint
55
from 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

912
def 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

2222
def 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

4439
def 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

5245
def 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

6153
print(upload_files(bucketName))
6254
print(list_files(bucketName))
63-
print(download_random_file(bucketURI,
64-
bucketName,
65-
bucketFolder))
55+
print(download_random_file(bucketName,
56+
bucketFolder,
57+
localFolder))
6658
print(rename_file(bucketName,
6759
bucketFolder,
6860
'test.csv',

0 commit comments

Comments
 (0)