Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Question: how to make the uploaded image public after uploading #21

Closed
MaeseppTarvo opened this issue Nov 30, 2017 · 12 comments
Closed

Comments

@MaeseppTarvo
Copy link

MaeseppTarvo commented Nov 30, 2017

Hey, I have a question. I am trying to upload image and make it public. I've made the right permissions to my bucket but for some reason I am getting exception:

{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}

This is how I upload and it gives me the above error:

foreach($request->file('post_image') as $image){
               $filename = $post->id.'_'.$i.'.jpg';
               $location = 'posts/'.$post->id;
               $storage = Storage::disk('gcs')->setVisibility($filename, 'public')->put($location, $image);
               $i++;
}

But it works like this:

foreach($request->file('post_image') as $image){
               $filename = $post->id.'_'.$i.'.jpg';
               $location = 'posts/'.$post->id;
               $storage = Storage::disk('gcs')->put($location, $image);
               $i++;
}
@zoidyzoidzoid
Copy link
Contributor

And if you try set visibility afterwards?

@MaeseppTarvo
Copy link
Author

@zoidbergwill If I do like that:

$storage = Storage::disk('gcs')->put($location, $image);
$storage->setVisibility($filename, 'public');

I get

Call to a member function setVisibility() on string

@zoidyzoidzoid
Copy link
Contributor

And what about the following?

Storage::disk('gcs')->setVisibility($location, 'public');

@zoidyzoidzoid
Copy link
Contributor

This looks like a dupe of #8, I think.

We should improve the docs, and errors if possible.

@MaeseppTarvo
Copy link
Author

MaeseppTarvo commented Nov 30, 2017

@zoidbergwill yep it is pretty much the same, however I am getting errors while setting the visibility. If I do as you said then I get the following error:

403 Insufficient Permission

Which is weird because I have read & write permissions set on my bucket. I even tried to give "all" permissions. The result is the same.

@zoidyzoidzoid
Copy link
Contributor

What permissions does the service account you're using have?

@MaeseppTarvo
Copy link
Author

Those are the permissions:

screen shot 2017-11-30 at 17 18 52

@zoidyzoidzoid
Copy link
Contributor

Weird. Not sure how those match to either of these:

https://cloud.google.com/storage/docs/access-control/iam-roles
https://cloud.google.com/storage/docs/access-control/iam-permissions

Ahhhhh, the following page mentions that you need Storage Owner for editing bucket metadata and ACLs:

https://cloud.google.com/storage/docs/access-control/lists#permissions

I assume it means bucket and object ACLs.

@zoidyzoidzoid
Copy link
Contributor

Another option would be using a bucket with a default object ACL of public?

@MaeseppTarvo
Copy link
Author

MaeseppTarvo commented Dec 3, 2017

I tried the following: gsutil iam ch allUsers:objectViewer gs://ex-bucket but it still gives me the same error. Followed the documentation:

https://cloud.google.com/storage/docs/gsutil/commands/iam

@varqasim
Copy link

varqasim commented Apr 12, 2018

This worked for me

$images_path = 'pool_images/' . $pool->id . '/';
            $storage = Storage::disk('gcs');

            $image = $request->file('image');
            $image_new_name = str_replace(' ', '_', $image->getClientOriginalName());

            $storage->putFileAs($images_path, $image, $image_new_name, 'public');

Using putFileAs method and passing 'public' as a parameter

@ultrono
Copy link

ultrono commented Feb 28, 2020

Just in case anyone else comes here from Google, I was having a similar issue. I uploaded files without issue and set the visibility to public. However, when viewing the public url I was prompted to login to my Google account.

This was caused by me using an incorrect base url. I was using https://storage.cloud.google.com/bucket/file.txt instead of https://storage.googleapis.com/bucket/file.txt. using the latter url resolved my issues.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants