Skip to content

Instantly share code, notes, and snippets.

@amonks
Last active May 10, 2016 04:49
Show Gist options
  • Save amonks/9e8fd9792095569869ba9f1bf090e394 to your computer and use it in GitHub Desktop.
Save amonks/9e8fd9792095569869ba9f1bf090e394 to your computer and use it in GitHub Desktop.

1. install the amazon library

http://docs.aws.amazon.com/aws-sdk-php/v2/guide/installation.html#installing-using-the-zip-archive

2. upd8 ur code

When someone uploads a new jawn, you'll make a json file and upload it to amazon s3.

Rather than hitting up a getlist.php file on your server, the browser extension will get the file from s3.

So, we're gonna sort-of combine your getlist.php and a little additional code into upload.php.

code

this chunk of code will log into amazon. I'll send you the KEY_ID and ACCESS_KEY separately.

// this code first

use Aws\S3\S3Client;

// Instantiate the S3 client with your AWS credentials
$s3Client = S3Client::factory(array(
    'credentials' => array(
        'key'    => 'YOUR_AWS_ACCESS_KEY_ID',
        'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
    )
));

this code should run every time someone uploads an image. It'll do a few things.

  1. keep a file called "list.txt", and add a line to the end of it for every upload
  2. then, it'll read that file and upload it to s3
// use a file to keep track of the images
$list_file = fopen("list.txt", "a+");

// add the new image to the end of the file
$txt = "something\n";        // "\n" means "the end of a line"
fwrite($list_file, $txt);

// now the file has all the lines from previous uploads, plus the new line from this one

// read the file into a variable
$the_list = fread($list_file, filesize("list.txt"));

// upload the list to amazon s3
// we made $s3client in the first code chunk
// s3 calls files "objects", so putObject means "upload a file"
$result = $s3client->putObject(array(
    'Bucket' => 'zingularity.int3rn3t.website',  // the file will end up at 
    'Key'    => 'list.json',   // << filename    // zingularity.int3rn3t.website/list.json
    'Body'   => $the_list,     // <<< this is the text of the actual file. see above.
    'ContentType' => 'application/json'          // tell amazon that it's json
));

fclose($list_file);

wait hold on, what's s3?

s3 stands for "simple storage service". You make "buckets," which are kinda like folders, and then you put "objects" in them, which are exactly like files.

You can make a bucket public and point a domain name at it to host a website. If you did so, and your bucket was called "fabulousartist.com" and then an object called "blog/my-first-post.html", you'd be able to browse to "http://fabulousartist.com/blog/my-first-post.html" and see that file.

You can make a bucket private or write-only to make a file dropbox.

It's very cheap: 3 cents per gigabyte in storage and a penny per 25,000 requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment