s_p
s_p

Reputation: 4693

AWS upload to S3 with SQS - PHP syntax

Would uploading to S3 using SQS make the process more fault tolerant?

If so, i am having a hard time with syntax, trying to combine creating a queue then uploading to S3.
If my logic is not correct, how would i set up a system using SQS to upload to S3?

if (!class_exists('S3'))require_once('S3.php');

// *these keys are random strings
$AWS_KEY = "6VVWTU4JDAAKHYB1C3ZN";
$AWS_SECRET_KEY = "GMSCUD8C0QA1QLV9Y3RP2IAKDIZSCHRGKEJSXZ4F";

//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', $AWS_KEY);
if (!defined('awsSecretKey')) define('awsSecretKey', $AWS_SECRET_KEY);
//instantiate the class
$s3 = new S3(awsAccessKey, awsSecretKey);

//check whether a form was submitted
if(isset($_POST['Submit'])){

    //retreive post variables
    $fileName = $_FILES['theFile']['name'];
    $fileTempName = $_FILES['theFile']['tmp_name'];

    //create a new bucket
    $s3->putBucket("mybucket", S3::ACL_PUBLIC_READ);




    //add the queue
    $sqs = new AmazonSQS(array( "key" => $AWS_KEY, "secret" => $AWS_SECRET_KEY ));
    $response = $sqs->create_queue('test-topic-queue');
    $queue_url = (string) $response->body->CreateQueueResult->QueueUrl;
    $queue_arn = 'arn:aws:sqs:us-east-1:ENCQ8gqrAcXv:test-topic-queue';

    //$queue_url . ?Action=SendMessage&MessageBody=Your%20Message%20Text?&AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE&Version=2011-10-01?&Expires=2008-02-10T12:00:00Z?&Signature=lBP67vCvGlDMBQ1do?fZxg8E8SUEXAMPLE&SignatureVersion=2&SignatureMethod=HmacSHA256


    // HOW DO I INCORPORATE SQS AND S3



    //move the file
    if ($s3->putObjectFile($fileTempName, 
                                "mybucket", 
                                "myFolder/" . $fileName, S3::ACL_PUBLIC_READ, 
                                array(), 
                                $_FILES['theFile']['type']) ) {
        //it works
    }else{
        // error
    }
}

Upvotes: 0

Views: 5156

Answers (2)

Destreyf
Destreyf

Reputation: 461

I think i figured out the OP's confusion on this topic.

The diagram shown makes it appear that SQS is handling uploads, but its really not, when you upload 1 or 100 photo's, they're added to S3 directly, then using SQS it creates a "Task" which one of the EC2 "Processing Servces" will pull, and then grab the actual picture from the S3 storage that as named in the SQS message.

Hopefully this gives some insight for future users who see this question feeling lost.

Upvotes: 1

Tyler
Tyler

Reputation: 1285

I'm not exactly understanding the fault tolerance you are requesting. But in terms of using S3 and SQS for scaling, there is an excellent paper on the Amazon AWS website that talks about scaling up and down your infrastructure using SQS and EC2 together, which can of course include processes like uploading to S3 and using SQS to tell the application to process something. You don't mention whether or not you're using EC2 or if this is of interest.

Here is the article: http://aws.amazon.com/articles/1464

Otherwise, it sounds like your logic may be confused as SQS isn't an in-between from server to S3, but rather more for application messaging.

Upvotes: 4

Related Questions