2

I uploaded a CSV file test.csv to Google Cloud Storage's bucket. The resulting public_url is like this

https://storage.googleapis.com/mybucket/test-2017-04-11-025727.csv

Originally `test.csv' has some row and column containing numbers like this

6,148,72,35,0,33.6,0.627,50,1 
8,183,64,0,0,23.3,0.672,32,1
...
...

I uploaded the file by referring to bookshelf tutorial --> https://github.com/GoogleCloudPlatform/getting-started-python no 6-pubsub. The uploaded file will be saved with timestamp added to it.

Now I want to download the file that I uploaded to the bucket by using requests

Here is what I've been working on. The original sample is at 6-pubsub/bookshelf/crud.py. Below is script that I already edited based on the original sample.

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)

    ...
    ...

I opened folder temp and check the testdata.csv. It shows me an error like this inside the CSV file.

<?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>

I was hoping testdata.csv will have same contents like test.csv but it did not.

I already recheck my OAuth client and secret, bucket id on config.py but the error still there.

How do I solve this kind of error?

Thank you in advanced.

Ling
  • 891
  • 5
  • 17
  • 40
  • It sounds like your object is not publicly accessible. You'll want to mark it as publicly readable, either in the console or by setting the ACL when you upload it. – Brandon Yarbrough Apr 11 '17 at 05:05

1 Answers1

0

I solved it. It is just like mr @Brandon Yarbrough said that the bucket's object is not publicly readable.

To make the bucket public, taking from this link --> https://github.com/GoogleCloudPlatform/gsutil/issues/419

gsutil defacl set public-read gs://<bucket_name>
Ling
  • 891
  • 5
  • 17
  • 40