Skip to content

Instantly share code, notes, and snippets.

@jamiehannaford
Created November 14, 2013 13:53
Show Gist options
  • Save jamiehannaford/7467137 to your computer and use it in GitHub Desktop.
Save jamiehannaford/7467137 to your computer and use it in GitHub Desktop.
public function uploadObjects(array $files, array $commonHeaders = array())
{
$requests = $entities = array();
foreach ($files as $entity) {
if (empty($entity['name'])) {
throw new Exceptions\InvalidArgumentError('You must provide a name.');
}
if (!empty($entity['path']) && file_exists($entity['path'])) {
$body = fopen($entity['path'], 'r+');
} elseif (!empty($entity['body'])) {
$body = $entity['body'];
} else {
throw new Exceptions\InvalidArgumentError('You must provide either a readable path or a body');
}
$entityBody = $entities[] = EntityBody::factory($body);
// @codeCoverageIgnoreStart
if ($entityBody->getContentLength() >= 5 * Size::GB) {
throw new Exceptions\InvalidArgumentError(
'For multiple uploads, you cannot upload more than 5GB per '
. ' file. Use the UploadBuilder for larger files.'
);
}
// @codeCoverageIgnoreEnd
// Allow custom headers and common
$headers = (isset($entity['headers'])) ? $entity['headers'] : $commonHeaders;
$url = clone $this->getUrl();
$url->addPath($entity['name']);
$requests[] = $this->getClient()->put($url, $headers, $entityBody);
}
$responses = $this->getClient()->send($requests);
foreach ($entities as $entity) {
$entity->close();
}
return $responses;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment