2017-07-03 4 views
1

Ich versuche, eine JSON hochladen ich habe Google Cloud-Speicher. Ich kann es manuell tun, also weiß ich, dass es funktioniert, aber jetzt möchte ich ein Python-Skript schreiben, das es automatisch macht.Hochladen eines JSON in Google Cloud-Speicher über 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') 

ist der Code, den ich bisher habe, und das ist der Fehler, den ich bekomme.

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' 

Antwort

2

Sie sollten eine geöffnete Datei upload_from_file nicht String übergeben, nur um

with open('t.json', 'rb') as f: 
    blob.upload_from_file(f) 
ändern
+0

danke das hat funktioniert! –