CodePlateau
CodePlateau

Reputation: 391

How to copy an image to Amazon S3?

I have a problem a copying an image to Amazon S3

I am using the PHP copy function to copy the image from one server to other server ..it works on go-daddy host server. But it doesn't work for S3. Here is the code that is not working:

$strSource =http://img.youtube.com/vi/got6nXcpLGA/hqdefault.jpg

copy($strSource ,$dest);

$dest is my bucket url with folder present to upload images

Upvotes: 1

Views: 5353

Answers (3)

user1568058
user1568058

Reputation:

Here is my examnple from the documentation to copy an object in S3 Bucket

public function copyObject($sSourceKey, $sDestKey)
    {
        $this->checkKey($sSourceKey);
        $this->checkKey($sDestKey);

        $bRet = false;

        // http://docs.aws.amazon.com/aws-sdk-php-2/latest/class-Aws.S3.S3Client.html#_copyObject
        try {
            $response = $this->_oS3Client->copyObject(
                array(
                    'Bucket' => $this->getBucketName(),
                    'Key' => $sDestKey,
                    'CopySource' => urlencode($this->getBucketName() . '/' . $sSourceKey),
                )
            );

            if (isset($response['LastModified'])) {
                $bRet = true;
            }
        } catch (Exception $e) {

            $GLOBALS['error'] = 1;
            $GLOBALS["info_msg"][] = __METHOD__ . ' ' . $e->getMessage();
            $bRet = false;
        }

        return $bRet;
    }

Upvotes: 0

Edwin
Edwin

Reputation: 1468

Not sure if you have used the AWS PHP SDK, but the AWS SDKs can come in handy in situations like this. The SDK can be used in conjunction with IAM roles to grant access to your S3 bucket. These are the steps:

  1. Modify your code to use the PHP SDK to upload the files (if needed).
  2. Create an IAM Role and grant the role permission to the needed S3 buckets.
  3. When you start your EC2 instance, specify that you want to use the role.

Then your code will automatically use the permissions that you grant that role. IAM gives the instance temporary credentials that the SDK uses. These credentials are automatically rotated for you by IAM and EC2.

Upvotes: 0

web-nomad
web-nomad

Reputation: 6013

I am not sure you could copy an image to AWS just like that. I would suggest using a library which talks to the AWS server and then running your commands.

Check this - http://undesigned.org.za/2007/10/22/amazon-s3-php-class

It provides a REST implementation for AWS.

For example, if you want to copy your image, you can do:

$s3 = new S3($awsAccessKey, $awsSecretKey);
$s3->copyObject($srcBucket, $srcName, $bucketName, $saveName, $metaHeaders = array(), $requestHeaders = array());

$awsAccessKey and $awsSecretKey are your secret keys for AWS a/c.

Check it out and hope it helps.

Upvotes: 2

Related Questions