[英]How to download an object from google cloud storage bucket?
我將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
相同的內容,但沒有。
我已經重新檢查了我的OAuth客戶端和機密,以及config.py
上的存儲區ID,但錯誤仍然存在。
我該如何解決這種錯誤?
在此先感謝您。
我解決了 就像@Brandon Yarbrough先生說的那樣,存儲桶的對象不可公開讀取。
要公開存儲桶,請點擊此鏈接-> https://github.com/GoogleCloudPlatform/gsutil/issues/419
gsutil defacl set public-read gs://<bucket_name>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.