簡體   English   中英

通過python將JSON上傳到谷歌雲存儲

[英]Uploading a JSON to google cloud storage via python

我正在嘗試將 JSON 上傳到谷歌雲存儲。 我可以手動完成,所以我知道它可以工作,但現在想編寫一個可以自動完成的 python 腳本。

import boto
import gcs_oauth2_boto_plugin
import os
import shutil
import StringIO
import tempfile
import time

from google.cloud import storage
from google.cloud.storage import blob

client = storage.Client(project='dataworks-356fa')
bucket = client.get_bucket('dataworks-356fa-backups')
blob = bucket.blob('t.json')
with open('t.json', 'rb') as f:
  blob.upload_from_file('f')

是我到目前為止的代碼,這是我得到的錯誤。

Traceback (most recent call last):
  File "uploadcloud.py", line 16, in <module>
    blob.upload_from_file('f')
  File "/Library/Python/2.7/site-packages/google/cloud/storage/blob.py", line 891, in upload_from_file
    client, file_obj, content_type, size, num_retries)
  File "/Library/Python/2.7/site-packages/google/cloud/storage/blob.py", line 815, in _do_upload
    client, stream, content_type, size, num_retries)
  File "/Library/Python/2.7/site-packages/google/cloud/storage/blob.py", line 634, in _do_multipart_upload
    data = stream.read()
AttributeError: 'str' object has no attribute 'read'

您應該將打開的文件傳遞給upload_from_file而不是字符串,只需更改為

with open('t.json', 'rb') as f:
  blob.upload_from_file(f)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM