/** * Uploads string as file to s3 * @param $name * @param $content * @param string $contentType * @return string */ public function uploadString($name, $content, $contentType = 'text/plain') { $s3FileName = $this->getFilePathAndUniquePrefix() . $name; list($bucket, $prefix) = explode('/', $this->s3path, 2); $this->s3client->putObject(['Bucket' => $bucket, 'Key' => (empty($prefix) ? '' : trim($prefix, '/') . '/') . $s3FileName, 'ContentType' => $contentType, 'ACL' => 'private', 'ServerSideEncryption' => 'AES256', 'Body' => $content]); return $this->withUrlPrefix($s3FileName); }
/** * Write data to a specific relative location * * @param mixed $data Data to be written * @param string $filename File name * @param string $uploadDir Relative file path * @return string Relative path to created file, false if there were errors */ public function write($data, $filename = '', $uploadDir = '') { // Upload data to Amazon S3 $this->client->putObject(array('Bucket' => $this->bucket, 'Key' => $uploadDir . '/' . $filename, 'Body' => $data, 'CacheControl' => 'max-age=' . $this->maxAge, 'ACL' => 'public-read')); // Build absolute path to uploaded resource return $this->bucketURL . '/' . (isset($uploadDir[0]) ? $uploadDir . '/' : ''); }
public function putObject($bucket, $key, $filePath, $options) { $options['Bucket'] = $bucket; $options['Key'] = $key; $options['Body'] = EntityBody::factory(fopen($filePath, 'r+')); return $this->s3->putObject($options); }
/** * {@inheritdoc} */ public function put($path, $data) { try { $this->s3->putObject(['Bucket' => $this->bucket, 'Key' => $path, 'Body' => $data]); } catch (AwsExceptionInterface $e) { throw Exception\StorageException::putError($path, $e); } }
/** * Move an uploaded file to it's intended destination. * * @param string $file * @param string $filePath */ public function move($file, $filePath) { $objectConfig = $this->attachedFile->s3_object_config; $fileSpecificConfig = ['Key' => $filePath, 'SourceFile' => $file, 'ContentType' => $this->attachedFile->contentType()]; $mergedConfig = array_merge($objectConfig, $fileSpecificConfig); $this->ensureBucketExists($mergedConfig['Bucket']); $this->s3Client->putObject($mergedConfig); }
/** * @param string $path * @param string|resource $data * @param string $contentType * * @return bool * @throws FileException */ public function save($path, $data, $contentType = null) { try { $this->s3->putObject(['Bucket' => $this->bucket, 'Key' => $path, 'Body' => $data, 'ContentType' => $contentType]); } catch (\Exception $e) { throw new FileException('Unable to save file', $e->getCode(), $e); } return true; }
public function set($key, $value, $ttl = null) { $this->load(); try { $this->s3Client->putObject(["Bucket" => S3Cache::BUCKET, "Key" => $key, "Body" => $value, "Expires" => $this->ttlToDateTime($ttl, $this->defaultTTL)]); } catch (\Exception $e) { throw new \RuntimeException("Unable to set object in S3Cache '{$key}''"); } }
/** * @param $filename * @param $filePath * @param string $mimeType * @return \Aws\Result|null */ public function upload($filename, $filePath, $mimeType = 'image/jpeg') { $result = null; try { $result = $this->s3->putObject(['Bucket' => $this->bucket, 'Key' => $filename, 'SourceFile' => $filePath, 'ACL' => self::ACL_PRIVATE_READ, 'ContentType' => $mimeType]); } catch (\Exception $e) { } return $result; }
/** * S3の指定パスへファイルをアップロードする * * @param string $from_path * @param string $to_path * @return void **/ public function upload($from_path, $to_path) { try { $this->client->putObject(array('Bucket' => $this->bucket, 'Key' => $to_path, 'Body' => EntityBody::factory(fopen($from_path, 'r')))); } catch (\Exception $e) { throw $e; } return true; }
/** * {@inheritDoc} */ public function store(BinaryInterface $binary, $path, $filter) { $objectPath = $this->getObjectPath($path, $filter); try { $this->storage->putObject(array('ACL' => $this->acl, 'Bucket' => $this->bucket, 'Key' => $objectPath, 'Body' => $binary->getContent(), 'ContentType' => $binary->getMimeType())); } catch (\Exception $e) { $this->logError('The object could not be created on Amazon S3.', array('objectPath' => $objectPath, 'filter' => $filter, 'bucket' => $this->bucket, 'exception' => $e)); throw new NotStorableException('The object could not be created on Amazon S3.', null, $e); } }
/** * @param string $html the content to be uploaded. * @return string the URL of the uploaded content. */ public function uploadHtml($headers, $html) { $file = date('YmdHis-') . uniqid("", true); $key = (!empty($this->directoryPath) ? $this->directoryPath . '/' : '') . $file . '.html'; $url = $this->_client->getObjectUrl($this->bucket, $key); $result = $this->_client->putObject(array_merge(['ACL' => 'public-read', 'Bucket' => $this->bucket, 'Body' => str_replace($this->archiveUrlTag, $url, $html), 'CacheControl' => 'max-age=31536000, public', 'ContentType' => 'text/html', 'Key' => $key, 'Metadata' => ['X-UID-MailHeader' => \yii\helpers\Json::encode($headers)], 'Expires' => gmdate('D, d M Y H:i:s \\G\\M\\T', strtotime('+5 year'))], $this->uploadOptions)); if ($result) { return $url; } else { return false; } }
public function uploadMedia(Media $media, $acl = 'public-read') { $targetPath = parse_url($media->getUrl(), PHP_URL_PATH); if ($media->getUrl() === $targetPath) { throw new \InvalidArgumentException('Media has to have a public URL set before uploading'); } if (null === $media->getContent()) { throw new \RuntimeException('Dunno how to get file contents'); } $fd = fopen($media->getContent()->getRealPath(), 'rb'); $storageResponse = $this->storage->putObject(['ACL' => $acl, 'Bucket' => $this->bucketName, 'Key' => ltrim($targetPath, '/'), 'Body' => $fd, 'ContentType' => $media->getContentType(), 'CacheControl' => 'public, max-age=283824000', 'Expires' => gmdate('D, d M Y H:i:s T', strtotime('+9 years'))]); fclose($fd); return $storageResponse->get('ObjectURL'); }
/** * {@inheritDoc} * * @link http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpPHP.html */ public function store($content) { $relativePath = $this->generateStoragePath($content); $absolutePath = $this->getConfigRelativeKey('s3BucketSubfolder') . '/' . $relativePath; // Note: The S3 doesS3ObjectExist method has a problem when the object doesn't exist within the sdk, so we skip this check for now // if(! $this->doesS3ObjectExist($this->getConfigRelativeKey('s3Bucket'),, $absolutePath)) { try { $this->s3->putObject(array('Bucket' => $this->getConfigRelativeKey('s3Bucket'), 'Key' => $absolutePath, 'Body' => $content)); } catch (S3Exception $e) { echo "There was an error uploading the file: " . $e->getMessage() . PHP_EOL; $absolutePath = false; } // } return $absolutePath; }
/** * @depends testPutAndListObjects */ public function testPutObjectsWithUtf8Keys() { self::log("Uploading an object with a UTF-8 key"); $key = 'åbc'; $this->client->putObject(array('Bucket' => $this->bucket, 'Key' => $key, 'Body' => 'hi')); $this->client->waitUntil('object_exists', "{$this->bucket}/{$key}"); }
/** * @param string $file_filename * @param string $fileDe * @param string $filePara * @param string $file_mimetype * @param string $acl * @return \Guzzle\Service\Resource\Model */ public function send($file_filename, $fileDe, $filePara, $file_mimetype = null, $acl = \Aws\S3\Enum\CannedAcl::PUBLIC_READ) { if ($file_mimetype == null) { $file_mimetype = $this->getMimeType($file_filename); } $filePara = str_replace('/_s/', '/', $filePara); if (!class_exists('S3Util')) { require_once __DIR__ . '/S3Util.php'; } $sendObject = array('Bucket' => $this->bucket_name, 'SourceFile' => $fileDe, 'Key' => S3Util::getPrefix() . $filePara, 'ACL' => $acl, 'CacheControl' => 'max-age=1296000', 'Expires' => date('r', mktime(0, 0, 0, date('m'), date('d'), date('Y') + 10)), 'Etag' => md5($filePara), 'ContentType' => $file_mimetype, 'StorageClass' => \Aws\S3\Enum\StorageClass::STANDARD, 'ContentDisposition' => 'filename="' . $file_filename . '"'); try { return $this->client->putObject($sendObject); } catch (Exception $e) { } return null; }
/** * Write using a stream * * @param string $path * @param resource $resource * @param mixed $config * * @return array file metadata */ public function writeStream($path, $resource, $config = null) { $config = Util::ensureConfig($config); $options = $this->getOptions($path, array('Body' => $resource), $config); $this->client->putObject($options); return $this->normalizeObject($options); }
public function submitForm(array &$form, FormStateInterface $form_state) { $category = $form_state->getValue('category'); $header_bg = $form_state->getValue('header_bg')[0]; $fid = $form_state->getValue('header_bg')[0]; $filename = db_select('file_managed', 'f')->condition('f.fid', $fid, '=')->fields('f', array('filename'))->execute()->fetchField(); $data = db_select('file_managed', 'f')->condition('f.fid', $fid, '=')->fields('f', array('uri'))->execute()->fetchField(); $config = $this->config('amazons.settings'); $AccessKeyId = $config->get('AccessKeyId'); $SecretAccessKey = $config->get('SecretAccessKey'); $region = $config->get('region'); $bucket = $config->get('bucketname'); $s3Client = new \Aws\S3\S3Client(['version' => 'latest', 'region' => $region, 'credentials' => ['key' => $AccessKeyId, 'secret' => $SecretAccessKey]]); //Creating bucket dynamically // $result1 = $s3Client->createBucket(array('Bucket' => 'mybucket')); // // echo "<pre>"; // print_r($result1); // exit; //$bucket = 'ncdfiles'; $key = date('ymdhis') . time() . $filename; //$data = 'public://ncdfiles/' . $filename; $result = $s3Client->putObject(array('Bucket' => $bucket, 'Key' => $key, 'SourceFile' => $data, 'ACL' => 'public-read')); db_insert('amazon_s3_data')->fields(array('cat_name' => $category, 'file_path' => $result['ObjectURL'], 'fid' => $fid))->execute(); drupal_set_message('File uploaded to AWS server'); return; }
/** * Write data to a file. * * @param string $file * @param string|array|resource $data * @return boolean * @throws \RuntimeException */ public function write($file, $data) { if (null !== $this->filenameFilter) { $file = $this->filenameFilter->filter($file); } if (is_resource($data)) { $type = $this->getMimeTypeDetector()->detectFromResource($data); } else { if (is_array($data)) { $data = implode('', $data); } $type = $this->getMimeTypeDetector()->detectFromString($data); } $params = array('Bucket' => $this->getBucket(), 'Key' => $file, 'Body' => $data, 'ACL' => $this->getAcl(), 'StorageClass' => $this->getStorageClass()); if (!empty($type)) { $params['ContentType'] = $type; } try { $this->s3Client->putObject($params); } catch (S3Exception $e) { if (!$this->getThrowExceptions()) { return false; } throw new \RuntimeException('Exception thrown by Aws\\S3\\S3Client: ' . $e->getMessage(), null, $e); } return true; }
/** * Write a string to a file * * @param string $path file path * @param string $content new file content * @return bool * @author Dmitry (dio) Levashov **/ protected function _filePutContents($path, $content) { $newkey = $this->_normpath($path); $newkey = preg_replace("/\\/\$/", "", $newkey); $this->s3->putObject(['Bucket' => $this->options['bucket'], 'Key' => $newkey, 'Body' => $content, 'ACL' => CannedAcl::PUBLIC_READ]); return true; }
/** * Adds an object to a bucket. * * @param string $bucket * @param string $key * @param string $content * @param array $params * * @return mixed */ public function putObject($bucket, $key, $content, array $params = []) { $params['Bucket'] = $bucket; $params['Key'] = $key; $params['Body'] = $content; return $this->instance->putObject($params); }
/** * @param $file * @param $name * @param $mime * @return string */ protected function putFileOnAmazon($file, $name, $mime) { $arr = array('ACL' => 'public-read', 'Bucket' => $this->bucket, 'Key' => $name, 'Body' => is_resource($file) ? $file : file_get_contents($file), 'ValidateMD5' => false, 'ContentMD5' => false, 'ContentType' => $mime); /* @var \Guzzle\Service\Resource\Model $obj */ $obj = $this->client->putObject($arr); return str_replace('https://', 'http://', $obj->get('ObjectURL')); }
/** * Write a string to a file * * @param string $path file path * @param string $content new file content * @return bool * @author Dmitry (dio) Levashov **/ protected function _filePutContents($path, $content) { $newkey = $this->_normpath($path); $newkey = preg_replace("/\\/\$/", "", $newkey); $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION)); $this->s3->putObject(['Bucket' => $this->options['bucket'], 'Key' => $newkey, 'Body' => $content, 'ACL' => CannedAcl::PUBLIC_READ, 'ContentType' => self::$mimetypes[$ext]]); return true; }
/** * Copies the local file $filePath to DFS under the same name, or a new name * if specified * * @param string $srcFilePath Local file path to copy from * @param bool|string $dstFilePath * Optional path to copy to. If not specified, $srcFilePath is used * * @return bool */ public function copyToDFS($srcFilePath, $dstFilePath = false) { try { $this->s3client->putObject(array('Bucket' => $this->bucket, 'Key' => $dstFilePath ?: $srcFilePath, 'SourceFile' => $srcFilePath, 'ACL' => 'public-read')); return true; } catch (S3Exception $e) { eZDebug::writeError($e->getMessage(), __METHOD__); return false; } }
/** * @param $key string Name of the resource on S3 * @param $localPath string Local path of the file you want to upload * @param bool $isPublic Set the resource to public * @return bool|\Guzzle\Service\Resource\Model */ public function putFile($key, $localPath, $isPublic = false) { $options = array('Bucket' => $this->bucketName, 'Key' => $key, 'SourceFile' => $localPath); if ($isPublic) { $options['ACL'] = 'public-read'; } try { return $this->s3Client->putObject($options); } catch (\Exception $e) { return false; } }
/** * Write a string to a file * * @param string $path file path * @param string $content new file content * @return bool **/ protected function _filePutContents($path, $content) { $oldSettings = $this->getFile($path); $settings = array("Bucket" => $this->bucket, "Key" => $this->pathToKey($path), "ACL" => $this->options['acl'], "Body" => $content); if ($oldSettings) { $settings['Metadata'] = $oldSettings['Metadata']; $settings['ContentType'] = $oldSettings['ContentType']; $settings['ACP'] = Aws\S3\Model\Acp::fromArray($this->s3->getObjectAcl(array('Bucket' => $this->bucket, 'Key' => $this->pathToKey($path)))->toArray()); unset($settings['ACL']); } return $this->s3->putObject($settings); }
public function upload($target_name, $file, $content_type = 'text/json; charset=utf-8', $bucket = AWS_NETSUITE_SENDING_BUCKET) { $this->client = S3Client::factory(["key" => AWS_NETSUITE_INTEGRATION_KEY, "secret" => AWS_NETSUITE_INTEGRATION_SECRET]); $e = 0; try { $result = $this->client->putObject(array('Bucket' => $bucket, 'Key' => $target_name, 'SourceFile' => $file, 'ContentType' => $content_type, 'ACL' => 'private')); return true; } catch (Exception $e) { /* $this->out("There was an error uploading to S3"); */ } return false; }
/** * @param string $container * @param string $name * @param string $localFileName * @param string $type * * @throws DfException */ public function putBlobFromFile($container = '', $name = '', $localFileName = '', $type = '') { try { $this->checkConnection(); $options = ['Bucket' => $container, 'Key' => $name, 'SourceFile' => $localFileName]; if (!empty($type)) { $options['ContentType'] = $type; } $result = $this->blobConn->putObject($options); } catch (\Exception $ex) { throw new DfException('Failed to create blob "' . $name . '": ' . $ex->getMessage()); } }
/** * Create Folder * * @param string $folder Folder * * @return bool * @throws */ public function createFolder($folder) { $folder = trim($folder, "/") . "/"; if ($folder === "/") { return true; } // Fail if this pseudo directory key already exists if ($this->exists($folder)) { return true; } $response = $this->clientS3->putObject(array('Bucket' => $this->bucket, 'Key' => $folder, 'ACL' => static::ACL_PUBLIC_READ, 'Body' => "")); return (bool) $response; }
/** * @param $file * @param $fileName * @param $path * @param $fileType * @return mixed */ public function uploadThumbnail($file, $fileName, $path, $fileType) { $result['status'] = false; try { $uploadResult = $this->s3->putObject(['Body' => $file, 'Bucket' => $this->bucket, 'Key' => $path . '/' . $fileName, 'ContentType' => $fileType, 'ACL' => 'public-read', 'CacheControl' => '2592000']); $result['status'] = true; $result['objectUrl'] = $uploadResult['ObjectURL']; $result['uploadResult'] = $uploadResult; } catch (S3Exception $e) { echo $e . "\nThere was an error uploading the file.\n"; } return $result; }
public function writeBack($tmpFile) { if (!isset(self::$tmpFiles[$tmpFile])) { return false; } try { $result = $this->connection->putObject(array('Bucket' => $this->bucket, 'Key' => $this->cleanKey(self::$tmpFiles[$tmpFile]), 'SourceFile' => $tmpFile, 'ContentType' => \OC_Helper::getMimeType($tmpFile), 'ContentLength' => filesize($tmpFile))); $this->testTimeout(); unlink($tmpFile); } catch (S3Exception $e) { \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); return false; } }