I have uploaded images in an Amazon S3 bucket with CodeIgniter s3 library in my CI4 project. But the problem is need to set Access control list (ACL) permission individually in the image otherwise get AccessDenied error.
There should be some way to set the image permission while uploading the objects in Amazon S3 that I'm missing.
Here is the Screenshot of an Amazon S3 bucket's ACL, I do not make any folder yet for the images.
And this the ACL of manually set permission for the individual object in the bucket.
And Codeigniter code for uploading the image in the first place.
public function uploadFile($sourcePath, $destinationPath){
return $this->objectUploader($sourcePath, $destinationPath);
}
private function createClient(){
$this->s3Client = new S3Client([
'version' => 'latest',
'region' => $this->region,
'credentials' => [
'key' => $this->access_key_id,
'secret' => $this->secrect_access_key,
],
]);
}
private function putObject($path){
try {
$this->s3Client->putObject(array(
'Bucket'=> $this->bucket,
'Key' => basename($path),
'SourceFile' => $path,
'ContentType' => 'image',
'ACL' => 'public-read',
'StorageClass' => 'STANDARD'
));
} catch (S3Exception $e) {
// Catch an S3 specific exception.
$this->log_message($type = 'ERROR', $e->getMessage());
$this->data = $e->getMessage();
}
}
private function objectUploader($sourcePath, $destinationPath){
$source = fopen($sourcePath, 'rb');
$uploader = new ObjectUploader(
$this->s3Client,
$this->bucket,
$destinationPath,
$source
);
do {
try {
$result = $uploader->upload();
if ($result["@metadata"]["statusCode"] == '200') {
$this->data = $result;
return true;
}
return FALSE;
} catch (MultipartUploadException $e) {
rewind($source);
$uploader = new MultipartUploader($this->s3Client, $source, [
'state' => $e->getState(),
]);
$this->log_message($type = 'ERROR', $e->getMessage());
$this->data = $e->getMessage();
return FALSE;
}
} while (!isset($result));
}