Gaufrette\Adapter\AwsS3::write PHP Method

write() public method

public write ( $key, $content )
    public function write($key, $content)
    {
        $this->ensureBucketExists();
        $options = $this->getOptions($key, array('Body' => $content));
        /*
         * If the ContentType was not already set in the metadata, then we autodetect
         * it to prevent everything being served up as binary/octet-stream.
         */
        if (!isset($options['ContentType']) && $this->detectContentType) {
            $fileInfo = new \finfo(FILEINFO_MIME_TYPE);
            if (is_resource($content)) {
                $contentType = $fileInfo->file(stream_get_meta_data($content)['uri']);
            } else {
                $contentType = $fileInfo->buffer($content);
            }
            $options['ContentType'] = $contentType;
        }
        try {
            $this->service->putObject($options);
            if (is_resource($content)) {
                return Util\Size::fromResource($content);
            } else {
                return Util\Size::fromContent($content);
            }
        } catch (\Exception $e) {
            return false;
        }
    }

Usage Example

 public function testWritesObjects()
 {
     $mock = new MockPlugin(array(new Response(200), new Response(201)));
     $client = $this->getClient();
     $client->addSubscriber($mock);
     $adapter = new AwsS3($client, 'bucket');
     $this->assertEquals(7, $adapter->write('foo', 'testing'));
     $requests = $mock->getReceivedRequests();
     $this->assertEquals('bucket.s3.amazonaws.com', $requests[1]->getHost());
     $this->assertEquals('PUT', $requests[1]->getMethod());
 }
All Usage Examples Of Gaufrette\Adapter\AwsS3::write