1 回答
TA贡献1843条经验 获得超7个赞
这是为我工作的代码:
from google.cloud import storage
from pathlib import Path
storage_client = storage.Client()
bucket = storage_client.bucket('bucket')
for file in Path('/home/jupyter/folder').rglob('*.py'):
blob = bucket.blob(file.name)
blob.upload_from_filename(str(file))
print("File {} uploaded to {}.".format(file.name,bucket.name))
输出:
File file1.py uploaded to bucket.
File file2.py uploaded to bucket.
File file3.py uploaded to bucket.
编辑
或者,您可以使用:
import google.datalab.storage as storage
from pathlib import Path
bucket = storage.Bucket('bucket')
for file in Path('/home/jupyter/folder').rglob('*.py'):
blob = bucket.object(file.name)
blob.write_stream(file.read_text(), 'text/plain')
print("File {} uploaded to {}.".format(file.name,bucket.name))
输出:
File file1.py uploaded to bucket.
File file2.py uploaded to bucket.
File file3.py uploaded to bucket.
添加回答
举报