Skip to content

Instantly share code, notes, and snippets.

@jwcounts
Created October 19, 2022 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwcounts/b31cf28d7a6e8d89c3b39339288c7a8e to your computer and use it in GitHub Desktop.
Save jwcounts/b31cf28d7a6e8d89c3b39339288c7a8e to your computer and use it in GitHub Desktop.
Code example using Marco Arment's S3 Class
<?php
// You can either include or require the file. That will bring the class into your script
require "s3.php";
// Replace the variables below with your AWS credentials
$aws_key = 'YOUR AWS KEY';
$aws_secret = 'YOUR AWS SECRET';
$bucket_name = 'S3 BUCKET NAME';
$region = 'S3 BUCKET REGION';
$custom_endpoint = 'CUSTOM ENDPOINT'; // Can leave blank
// Creates new S3 object to work against
$s3 = new S3( $aws_key, $aws_secret, $bucket_name, $region, $custom_endpoint );
/**
* Uploading a file to the S3 bucket
*
* In this example, we're uploading a JSON file to https://[BUCKET_NAME].s3-[REGION].amazonaws.com/[PATH]
*
* ACLs (access control lists) can be found here: https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html
*/
$path = 'path/to/file.json';
$mime_type = 'application/json';
$acl = 'public-read';
$body = 'CONTENT GOES HERE';
$s3->put( $path, $mime_type, $acl, $body );
// You could then download the file like this
$file = $s3->get( $path );
// Or delete it
$s3->delete( $path );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment