create_object ( $bucket, $filename, $opt )

Creates an Amazon S3 object. After an Amazon S3 bucket is created, objects can be stored in it.

Each standard object can hold up to 5 GB of data. When an object is stored in Amazon S3, the data is streamed to multiple storage servers in multiple data centers. This ensures the data remains available in the event of internal network or hardware failure.

Access

public

Parameters

Parameter

Type

Required

Description

$bucket

string

Required

The name of the bucket to use.

$filename

string

Required

The file name for the object.

$opt

array

Optional

An associative array of parameters that can have the following keys:

  • acl - string - Optional - The ACL settings for the specified object. Accepts any of the following constants: [Allowed values: AmazonS3::ACL_PRIVATE, AmazonS3::ACL_PUBLIC, AmazonS3::ACL_OPEN, AmazonS3::ACL_AUTH_READ, AmazonS3::ACL_OWNER_READ, AmazonS3::ACL_OWNER_FULL_CONTROL]. Alternatively, an array of associative arrays. Each associative array contains an id and a permission key. The default value is ACL_PRIVATE.
  • body - string - Required; Conditional - The data to be stored in the object. Either this parameter or fileUpload must be specified.
  • contentType - string - Optional - The type of content that is being sent in the body. If a file is being uploaded via fileUpload as a file system path, it will attempt to determine the correct mime-type based on the file extension. The default value is application/octet-stream.
  • encryption - string - Optional - The algorithm to use for encrypting the object. [Allowed values: AES256]
  • fileUpload - string|resource - Required; Conditional - The URL/path for the file to upload, or an open resource. Either this parameter or body is required.
  • headers - array - Optional - Standard HTTP headers to send along in the request. Accepts an associative array of key-value pairs.
  • length - integer - Optional - The size of the object in bytes. For more information, see RFC 2616, section 14.13. The value can also be passed to the header option as Content-Length.
  • meta - array - Optional - An associative array of key-value pairs. Represented by x-amz-meta-:. Any header starting with this prefix is considered user metadata. It will be stored with the object and returned when you retrieve the object. The total size of the HTTP request, not including the body, must be less than 4 KB.
  • redirectTo - string - Optional - The URI to send an HTTP 301 redirect to when accessing this object. Value must be prefixed either /, http:// or https://.
  • seekTo - integer - Optional - The starting position in bytes within the file/stream to upload from.
  • storage - string - Optional - Whether to use Standard or Reduced Redundancy storage. [Allowed values: AmazonS3::STORAGE_STANDARD, AmazonS3::STORAGE_REDUCED]. The default value is STORAGE_STANDARD.
  • curlopts - array - Optional - A set of values to pass directly into curl_setopt(), where the key is a pre-defined CURLOPT_* constant.
  • returnCurlHandle - boolean - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.

Returns

Type

Description

CFResponse

A CFResponse object containing a parsed HTTP response.

Examples

Upload a string of content as a new object to S3.

// Instantiate the class
$s3 = new AmazonS3();
$bucket = 'my-bucket' . strtolower($s3->key);

$response = $s3->create_object($bucket, 'test1.txt', array(
	'body' => 'This is my body text.'
));

// Success?
var_dump($response->isOK());
Result:
bool(true)

Create an object with spaces, slashes, and UTF-8 characters in the filename.

// Instantiate the class
$s3 = new AmazonS3();
$bucket = 'my-bucket' . strtolower($s3->key);

$response = $s3->create_object($bucket, 'prefix with spaces/åéîøü/åéîøü/åéîøü with spaces.txt', array(
	'body' => 'This is my body text.'
));

// Success?
var_dump($response->isOK());
Result:
bool(true)

Upload a file instead of a string, and tweak some optional settings.

// Instantiate the class
$s3 = new AmazonS3();
$bucket = 'my-bucket' . strtolower($s3->key);

$response = $s3->create_object($bucket, 'üpløåd/î\'vé nøw béén üpløådéd.txt', array(
	'fileUpload' => 'upload_me.txt',
	// 'acl' => AmazonS3::ACL_PUBLIC,
	'contentType' => 'text/plain',
	'storage' => AmazonS3::STORAGE_REDUCED,
	'headers' => array(
		'Cache-Control'    => 'max-age',
		'Content-Encoding' => 'gzip',
		'Content-Language' => 'en-US',
		'Expires'          => 'Thu, 01 Dec 1994 16:00:00 GMT',
	),
	'grants' => array(
		array( 'id' => AmazonS3::USERS_AUTH,                                               'permission' => AmazonS3::GRANT_READ        ),
		array( 'id' => AmazonS3::USERS_AUTH,                                               'permission' => AmazonS3::GRANT_WRITE       ),
		array( 'id' => AmazonS3::USERS_ALL,                                                'permission' => AmazonS3::GRANT_WRITE       ),
		array( 'id' => 'my-email-address@domain.com',                                      'permission' => AmazonS3::GRANT_FULL_CONTROL),
		array( 'id' => '41f37e4803dbae41aa52290608a037dcf2309199b81c6257fe5570098b9e55a9', 'permission' => AmazonS3::GRANT_WRITE       ),
	),
	'meta' => array(
		'word'         => 'to your mother',    // x-amz-meta-word
		'ice-ice-baby' => 'too cold, too cold' // x-amz-meta-ice-ice-baby
	),
));

// Success?
var_dump($response->isOK());
Result:
bool(true)

Upload from an open file resource, leveraging seek position and upload length.

// Define a mebibyte
define('MB', 1048576);

// Instantiate the class
$s3 = new AmazonS3();
$bucket = 'my-bucket' . strtolower($s3->key);

// Open a file resource
$file_resource = fopen('large_video.mov', 'r');

// Upload a partial file
$response = $s3->create_object($bucket, 'large_video.mov', array(
	'fileUpload' => $file_resource,
	'seekTo' => 80*MB,
	'length' => 5*MB,
	'acl' => AmazonS3::ACL_PUBLIC
));

// Success?
var_dump($response->isOK());
Result:
bool(true)

Changelog

Version

Description

1.1

Added preliminary support for file I/O streams.

1.2.2

Added more robust handling for file I/O streams.

Related Methods

See Also

Source

Method defined in services/s3.class.php | Toggle source view (72 lines) | View on GitHub

public function create_object($bucket, $filename, $opt = null)
{
    if (!$opt) $opt = array();

    // Add this to our request
    $opt['verb'] = 'PUT';
    $opt['resource'] = $filename;

    // Handle content length. Can also be passed as an HTTP header.
    if (isset($opt['length']))
    {
        $opt['headers']['Content-Length'] = $opt['length'];
        unset($opt['length']);
    }

    // Handle content type. Can also be passed as an HTTP header.
    if (isset($opt['contentType']))
    {
        $opt['headers']['Content-Type'] = $opt['contentType'];
        unset($opt['contentType']);
    }

    // Handle Access Control Lists. Can also be passed as an HTTP header.
    if (isset($opt['acl']))
    {
        if (is_array($opt['acl']))
        {
            $opt['headers'] = array_merge($opt['headers'], $this->generate_access_policy_headers($opt['acl']));
        }
        else
        {
            $opt['headers']['x-amz-acl'] = $opt['acl'];
        }
    }

    // Handle storage settings. Can also be passed as an HTTP header.
    if (isset($opt['storage']))
    {
        $opt['headers']['x-amz-storage-class'] = $opt['storage'];
        unset($opt['storage']);
    }

    // Handle encryption settings. Can also be passed as an HTTP header.
    if (isset($opt['encryption']))
    {
        $opt['headers']['x-amz-server-side-encryption'] = $opt['encryption'];
        unset($opt['encryption']);
    }

    // URI to redirect to. Can also be passed as an HTTP header.
    if (isset($opt['redirectTo']))
    {
        $opt['headers']['x-amz-website-redirect-location'] = $opt['redirectTo'];
        unset($opt['redirectTo']);
    }

    // Handle meta tags. Can also be passed as an HTTP header.
    if (isset($opt['meta']))
    {
        foreach ($opt['meta'] as $meta_key => $meta_value)
        {
            // e.g., `My Meta Header` is converted to `x-amz-meta-my-meta-header`.
            $opt['headers']['x-amz-meta-' . strtolower(str_replace(' ', '-', $meta_key))] = $meta_value;
        }
        unset($opt['meta']);
    }

    $opt['headers']['Expect'] = '100-continue';

    // Authenticate to S3
    return $this->authenticate($bucket, $opt);
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback