Amazon_S3_And_CloudFront::check_write_permission PHP Method

check_write_permission() public method

Checks the user has write permission for S3
public check_write_permission ( string $bucket = null, string $region = null ) : boolean | WP_Error
$bucket string
$region string
return boolean | WP_Error
    function check_write_permission($bucket = null, $region = null)
    {
        if (is_null($bucket)) {
            if (!($bucket = $this->get_setting('bucket'))) {
                // if no bucket set then no need check
                return true;
            }
        }
        if (isset(self::$buckets_check[$bucket])) {
            return self::$buckets_check[$bucket];
        }
        $file_name = 'as3cf-permission-check.txt';
        $file_contents = __('This is a test file to check if the user has write permission to S3. Delete me if found.', 'amazon-s3-and-cloudfront');
        $path = $this->get_object_prefix();
        $key = $path . $file_name;
        $args = array('Bucket' => $bucket, 'Key' => $key, 'Body' => $file_contents, 'ACL' => 'public-read');
        try {
            // need to set region for buckets in non default region
            if (is_null($region)) {
                $region = $this->get_setting('region');
                if (is_wp_error($region)) {
                    return $region;
                }
            }
            // attempt to create the test file
            $this->get_s3client($region, true)->putObject($args);
            // delete it straight away if created
            $this->get_s3client()->deleteObject(array('Bucket' => $bucket, 'Key' => $key));
            $can_write = true;
        } catch (Exception $e) {
            // if we encounter an error that isn't access denied, throw that error
            if (!$e instanceof Aws\Common\Exception\ServiceResponseException || 'AccessDenied' !== $e->getExceptionCode()) {
                $error_msg = sprintf(__('There was an error attempting to check the permissions of the bucket %s: %s', 'amazon-s3-and-cloudfront'), $bucket, $e->getMessage());
                AS3CF_Error::log($error_msg);
                return new WP_Error('exception', $error_msg);
            }
            // write permission not found
            $can_write = false;
        }
        self::$buckets_check[$bucket] = $can_write;
        return $can_write;
    }
Amazon_S3_And_CloudFront