Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 115 lines (81 sloc) 4.136 kb
62884bb @tpyo
authored
1 #!/usr/local/bin/php
2 <?php
6249cf4 @tpyo Comment updates
authored
3 /**
4 * $Id$
5 *
6 * S3 class usage
7 */
62884bb @tpyo
authored
8
0154802 @tpyo Updated examples, renamed enableBucketLogging to setBucketLogging
authored
9 if (!class_exists('S3')) require_once 'S3.php';
62884bb @tpyo
authored
10
11 // AWS access info
12 if (!defined('awsAccessKey')) define('awsAccessKey', 'change-this');
13 if (!defined('awsSecretKey')) define('awsSecretKey', 'change-this');
14
0154802 @tpyo Updated examples, renamed enableBucketLogging to setBucketLogging
authored
15 $uploadFile = dirname(__FILE__).'/S3.php'; // File to upload, we'll use the S3 class since it exists
62884bb @tpyo
authored
16 $bucketName = uniqid('s3test'); // Temporary bucket
17
18 // If you want to use PECL Fileinfo for MIME types:
19 //if (!extension_loaded('fileinfo') && @dl('fileinfo.so')) $_ENV['MAGIC'] = '/usr/share/file/magic';
20
21
22 // Check if our upload file exists
23 if (!file_exists($uploadFile) || !is_file($uploadFile))
24 exit("\nERROR: No such file: $uploadFile\n\n");
25
26 // Check for CURL
27 if (!extension_loaded('curl') && !@dl(PHP_SHLIB_SUFFIX == 'so' ? 'curl.so' : 'php_curl.dll'))
28 exit("\nERROR: CURL extension not loaded\n\n");
29
30 // Pointless without your keys!
31 if (awsAccessKey == 'change-this' || awsSecretKey == 'change-this')
32 exit("\nERROR: AWS access information required\n\nPlease edit the following lines in this file:\n\n".
33 "define('awsAccessKey', 'change-me');\ndefine('awsSecretKey', 'change-me');\n\n");
34
35 // Instantiate the class
36 $s3 = new S3(awsAccessKey, awsSecretKey);
37
38 // List your buckets:
39 echo "S3::listBuckets(): ".print_r($s3->listBuckets(), 1)."\n";
40
41
42 // Create a bucket with public read access
43 if ($s3->putBucket($bucketName, S3::ACL_PUBLIC_READ)) {
44 echo "Created bucket {$bucketName}".PHP_EOL;
45
46 // Put our file (also with public read access)
47 if ($s3->putObjectFile($uploadFile, $bucketName, baseName($uploadFile), S3::ACL_PUBLIC_READ)) {
48 echo "S3::putObjectFile(): File copied to {$bucketName}/".baseName($uploadFile).PHP_EOL;
49
0154802 @tpyo Updated examples, renamed enableBucketLogging to setBucketLogging
authored
50
51 // Get the contents of our bucket
52 $contents = $s3->getBucket($bucketName);
53 echo "S3::getBucket(): Files in bucket {$bucketName}: ".print_r($contents, 1);
54
55
62884bb @tpyo
authored
56 // Get object info
57 $info = $s3->getObjectInfo($bucketName, baseName($uploadFile));
4852577 @binki Fix typo S3::getObjecInfo() -> S3::getObjectInfo() in example.
binki authored
58 echo "S3::getObjectInfo(): Info for {$bucketName}/".baseName($uploadFile).': '.print_r($info, 1);
62884bb @tpyo
authored
59
0154802 @tpyo Updated examples, renamed enableBucketLogging to setBucketLogging
authored
60
61 // You can also fetch the object into memory
62884bb @tpyo
authored
62 // var_dump("S3::getObject() to memory", $s3->getObject($bucketName, baseName($uploadFile)));
63
64 // Or save it into a file (write stream)
65 // var_dump("S3::getObject() to savefile.txt", $s3->getObject($bucketName, baseName($uploadFile), 'savefile.txt'));
66
67 // Or write it to a resource (write stream)
68 // var_dump("S3::getObject() to resource", $s3->getObject($bucketName, baseName($uploadFile), fopen('savefile.txt', 'wb')));
69
70
0154802 @tpyo Updated examples, renamed enableBucketLogging to setBucketLogging
authored
71
72 // Get the access control policy for a bucket:
73 // $acp = $s3->getAccessControlPolicy($bucketName);
74 // echo "S3::getAccessControlPolicy(): {$bucketName}: ".print_r($acp, 1);
75
ed0ef40 @tpyo Support for CloudFront and updates to setBucketLogging()
authored
76 // Update an access control policy ($acp should be the same as the data returned by S3::getAccessControlPolicy())
0154802 @tpyo Updated examples, renamed enableBucketLogging to setBucketLogging
authored
77 // $s3->setAccessControlPolicy($bucketName, '', $acp);
78 // $acp = $s3->getAccessControlPolicy($bucketName);
79 // echo "S3::getAccessControlPolicy(): {$bucketName}: ".print_r($acp, 1);
80
81
ed0ef40 @tpyo Support for CloudFront and updates to setBucketLogging()
authored
82 // Enable logging for a bucket:
83 // $s3->setBucketLogging($bucketName, 'logbucket', 'prefix');
0154802 @tpyo Updated examples, renamed enableBucketLogging to setBucketLogging
authored
84
85 // if (($logging = $s3->getBucketLogging($bucketName)) !== false) {
86 // echo "S3::getBucketLogging(): Logging for {$bucketName}: ".print_r($contents, 1);
87 // } else {
88 // echo "S3::getBucketLogging(): Logging for {$bucketName} not enabled\n";
89 // }
90
ed0ef40 @tpyo Support for CloudFront and updates to setBucketLogging()
authored
91 // Disable bucket logging:
92 // var_dump($s3->disableBucketLogging($bucketName));
93
62884bb @tpyo
authored
94
95 // Delete our file
96 if ($s3->deleteObject($bucketName, baseName($uploadFile))) {
97 echo "S3::deleteObject(): Deleted file\n";
98
99 // Delete the bucket we created (a bucket has to be empty to be deleted)
100 if ($s3->deleteBucket($bucketName)) {
101 echo "S3::deleteBucket(): Deleted bucket {$bucketName}\n";
102 } else {
103 echo "S3::deleteBucket(): Failed to delete bucket (it probably isn't empty)\n";
104 }
105
106 } else {
107 echo "S3::deleteObject(): Failed to delete file\n";
108 }
109 } else {
110 echo "S3::putObjectFile(): Failed to copy file\n";
111 }
112 } else {
113 echo "S3::putBucket(): Unable to create bucket (it may already exist and/or be owned by someone else)\n";
114 }
Something went wrong with that request. Please try again.