无法通过python脚本访问保存在谷歌存储桶路径中的文件

时间:2017-08-09 10:33:18

标签: python google-cloud-storage

我已将样本文件保存在GCS位置,例如:gs://MyBucket/Folder1/sampleData.csv

现在,我试图通过我的python脚本访问此文件,但我无法这样做。

python脚本:

import argparse
import json
import time
import uuid
import csv
from google.cloud import bigquery
from google.cloud.bigquery import SchemaField
from google.cloud import storage
import os
import sys
import googleapiclient.discovery


bigquery = googleapiclient.discovery.build('bigquery', 'v2') 

client = storage.Client()
bucket = client.get_bucket('MyBucket')
print bucket

blob = bucket.get_blob('gs://MyBucket/Folder1/sampleData.csv')
print(blob.download_as_string())

1 个答案:

答案 0 :(得分:1)

您正在尝试访问Google云端存储,但在范围内您提供了bigquery。

请参阅示例python脚本以列出文件并访问该对象。替换存储桶名称和对象名称并执行代码。它应该工作。

import argparse
import json

import googleapiclient.discovery

service = googleapiclient.discovery.build('storage', 'v1')


#### List Bucket
fields_to_return = \
        'nextPageToken,items(name,size,contentType,metadata(my-key))'
req = service.objects().list(bucket='XYZ', fields=fields_to_return)
resp = req.execute()
print resp

### Get Object 
req = service.objects().get_media(bucket='XYZ', object='ad_list04_08_2017.csv')
result=req.execute()
print result