john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

s3 example curl

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/local/bin/php
<?php

//cumulus s3 cloud on port 4288
// sudo apt-get install php5-cli php5-curl   # necessary dependencies
if (!class_exists('S3')) require_once 'S3.php';  // https://github.com/tpyo/amazon-s3-php-class

// Check for CURL
if (!extension_loaded('curl') && !@dl(PHP_SHLIB_SUFFIX == 'so' ? 'curl.so' : 'php_curl.dll'))
    exit("\nERROR: CURL extension not loaded" . PHP_EOL . PHP_EOL );


// AWS access info
//if (!defined('awsAccessKey')) define( 'awsAccessKey', 'Da0CG8QJHyFt0kHEXAMPLE' );
//if (!defined('awsSecretKey')) define( 'awsSecretKey', 'BYpW9CxjshFaQxYuDambHnNXZfnPsK0utzIEXAMPLE' );

//$s3 = new S3( awsAccessKey, awsSecretKey );   // Instantiate the class
//echo "S3::listBuckets(): ".print_r( $s3->listBuckets(), 1) . PHP_EOL; // List your buckets:
//echo "S3::listBuckets(): ".print_r( $btfs3->listBuckets( true ), 1) . PHP_EOL;    // detailed listing


$endpoint = 'example.com:4288';
$awsAccessKey = 'Da0CG8QJHyFt0kHEXAMPLE';
$awsSecretKey = 'BYpW9CxjshFaQxYuDambHnNXZfnPsK0utzIEXAMPLE';
$s3 = new S3( $awsAccessKey, $awsSecretKey, false, $endpoint );     // no ssl, custom s3 endpoint (not s3.amazonaws.com)


echo "S3::listBuckets(): ".print_r( $s3->listBuckets(), true ) . PHP_EOL;   // true: prevent display of print_r's return value


$bucketName = 'Files';
$uploadFile = dirname(__FILE__) . '/S3.php';   // File to upload, we'll use the S3 class since it must exist



$rest = new S3Request( 'GET', $bucketName, '', $endpoint );
$response = $rest->getResponse();
echo "GET BUCKET $bucketName , response code:" . $response->code . PHP_EOL . print_r( $response->headers , true ) . PHP_EOL;

echo print_r( simplexml_load_string( $response->body ) , true ) . PHP_EOL;  // assumes body exists and an xml response


$info = $s3->getObjectInfo( $bucketName, baseName($uploadFile) );
if ( $info ){
    echo "OBJECT INFO " . baseName($uploadFile) . ": " . print_r( $info, true );
} else {
    echo "UPLOADING $uploadFile to $bucketName" . PHP_EOL;
    $result = $s3->putObjectFile( $uploadFile, $bucketName, baseName($uploadFile), S3::ACL_PUBLIC_READ );
    echo "RESULT: $result" . PHP_EOL;
}

$randomFile = '1/1/brgz5z01wnvdtca/pidgin-xmpp-config.png';
$info = $s3->getObjectInfo( $bucketName, $randomFile );
echo "NOT EXIST $randomFile " . print_r( $info, true ) . PHP_EOL;


// OPTIONAL

//$s3->putBucket( $bucketName, S3::ACL_PUBLIC_READ )    //create a bucket

// If you want to use PECL Fileinfo for MIME types:
//if (!extension_loaded('fileinfo') && @dl('fileinfo.so')) $_ENV['MAGIC'] = '/usr/share/file/magic';


// DOES NOT WORK
//$contents = $s3->getBucket( $bucketName );        // does not work properly
//echo print_r( $contents ) . PHP_EOL;

  • « convert decimal to binary
  • phpunit json file kohana example »

Published

Sep 24, 2013

Category

php

~301 words

Tags

  • curl 1
  • example 36
  • php 82
  • s3 17