Reputation: 738
Sorry for the noob question but this is a problem I can't seem to crack. I have a laravel script to upload file publicly to a google storage bucket and it is working properly. The problem is that object that I upload is just inside the bucket, where it should be placed inside a directory inside the bucket. e.g bucket - /dev-bucket/ , correct directory - /dev-bucket/media/ below is my script
$client = new \Google_Client();
$credential = new \Google_Auth_AssertionCredentials(
'[email protected]',
['https://www.googleapis.com/auth/devstorage.full_control'],
file_get_contents(storage_path().'xxxxx.p12')
);
$client->setAssertionCredentials($credential);
// this access control is for project owners
$ownerAccess = new \Google_Service_Storage_ObjectAccessControl();
$ownerAccess->setEntity('project-owners-' . 'xxxxxxxxxxx');
$ownerAccess->setRole('OWNER');
// this access control is for public access
$readerAccess = new \Google_Service_Storage_ObjectAccessControl();
$readerAccess->setEntity('allUsers');
$readerAccess->setRole('READER');
$storage = new \Google_Service_Storage($client);
$obj = new \Google_Service_Storage_StorageObject();
$obj->setName($stageFileName);
$obj->setAcl([$ownerAccess, $readerAccess]);
$storage->objects->insert(
Config::get('constants.bucket'),
$obj,
[
'name' => $stageFileName,
'data' => file_get_contents($stageFile),
'uploadType' => 'media',
]
);
Is there an option on how to put the object inside the directory inside the bucket?
Upvotes: 1
Views: 1532
Reputation: 400
To upload a file to a specific folder in google bucket, I wrote below code:
Blob blob=
storage.create(BlobInfo.newBuilder(bucketName,"folder_name/"+f.getName()).setAcl(acls).build(),f.getInputStream());
And for downloading from that folder:
BlobId blobId = BlobId.of(bucketName, vendorName+"/"+fileName);
Blob blob = storage.get(blobId);
P.S. This is in JAVA, can do similar stuff in any other language.
Upvotes: 1
Reputation: 738
You should be able to upload files in the directory by pre-pending the file name of the object to be uploaded with the directory in the bucket for uploading. For example if you have a directory in your bucket called dev your filename should start with 'dev/{fileToBeUploaded}.txt'
Upvotes: 3