0

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.

enter image description here

And this the ACL of manually set permission for the individual object in the bucket.

enter image description here

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));
}
smac2020
  • 9,637
  • 4
  • 24
  • 38
Mahmud Hasan Jion
  • 445
  • 1
  • 5
  • 14
  • Yes, you can set `ACL=public-read` while uploading objects and they will be Public without requiring a Bucket Policy. Please note that you will need to deactivate S3 Block Public Access for ACLs to allow this to work. – John Rotenstein Jun 09 '21 at 05:55
  • sure @JohnRotenstein, did that too and also set the bucket policy using bucket policy generator. Now bucket objects are set to be public by default. – Mahmud Hasan Jion Jun 09 '21 at 07:58
  • Either one (Bucket Policy _or_ ACL on objects) is sufficient. You do not need to do both. – John Rotenstein Jun 09 '21 at 11:57

0 Answers0