我上传了一个CSV文件test.csv到Google Cloud Storage的存储桶中。生成的public_url如下所示
https://storage.googleapis.com/mybucket/test-2017-04-11-025727.csv最初,`test.csv‘有一些包含数字的行和列,如下所示
6,148,72,35,0,33.6,0.627,50,1
8,183,64,0,0,23.3,0.672,32,1
...
...我参考书架教程--> https://github.com/GoogleCloudPlatform/getting-started-python no 6-pubsub上传了这个文件。上传的文件将被保存,并添加时间戳。
现在,我想使用requests下载我上传到存储桶中的文件
这是我一直在做的事情。原始样本在6-pubsub/bookshelf/crud.py。下面是我根据原始示例编辑的脚本。
from machinelearning import get_model, oauth2, storage, tasks
from flask import Blueprint, current_app, redirect, render_template, request, session, url_for
import requests
import os
...
...
crud = Blueprint('crud', __name__)
save_folder = 'temp/'
def upload_csv_file(file):
...
...
return public_url
...
...
@crud.route('/add', methods=['GET', 'POST'])
def add():
data = request.form.to_dict(flat=True)
# This will upload the file that I pushed from local directory to GCS
if request.method == 'POST':
csv_url = upload_csv_file(request.files.get('file'))
if csv_url:
data['csvUrl'] = csv_url
# I think this is not working. This should download back the file and save it to a temporary folder inside current working directory
response = requests.get(public_url)
if not os.path.exists(save_folder):
os.makedirs(save_folder)
with open(save_folder + 'testdata.csv', 'wb') as f:
f.write(response.content)
...
...我打开了文件夹temp并检查了testdata.csv。它向我显示了CSV文件中的一个错误,如下所示。
<?xml version='1.0' encoding='UTF-8'?><Error><Code>AccessDenied</Code><Message>Access denied.</Message><Details>Anonymous users does not have storage.objects.get access to object mybucket/test-2017-04-11-025727.csv.</Details></Error>我希望testdata.csv能像test.csv一样有相同的内容,但它没有。
我已经在config.py上重新检查了我的OAuth客户端和密钥,存储桶id,但错误仍然存在。
如何解决这类错误?
感谢您的支持。
发布于 2017-04-11 16:06:30
我解决了。这就像@Brandon Yarbrough先生所说的,存储桶的对象是不可公开读取的。
要使存储桶公开,请从该链接获取--> https://github.com/GoogleCloudPlatform/gsutil/issues/419
gsutil defacl set public-read gs://<bucket_name>https://stackoverflow.com/questions/43336268
复制相似问题