/** * Create a link to a S3 object from a bucket. If expiration is not empty, then it is used to create * a signed URL * * @param string $object The object name (full path) * @param string $bucket The bucket name * @param string|int $expiration The Unix timestamp to expire at or a string that can be evaluated by strtotime * @throws InvalidDomainNameException * @return string */ public function __invoke($object, $bucket = '', $expiration = '') { $bucket = trim($bucket ?: $this->getDefaultBucket(), '/'); if (empty($bucket)) { throw new InvalidDomainNameException('An empty bucket name was given'); } if ($expiration) { $command = $this->client->getCommand('GetObject', ['Bucket' => $bucket, 'Key' => $object]); return $this->client->createPresignedRequest($command, $expiration)->getUri()->__toString(); } else { return $this->client->getObjectUrl($bucket, $object); } }
/** * Get a URL to the resource. * * @param \CipeMotion\Medialibrary\Entities\File $file * @param \CipeMotion\Medialibrary\Entities\Transformation|null $tranformation * @param bool $fullPreview * @param bool $download * * @return string */ public function getUrlForTransformation(File $file, Transformation $tranformation = null, $fullPreview = false, $download = false) { if (empty($tranformation)) { $tranformationName = 'upload'; $extension = $file->extension; if ($fullPreview && $file->type !== FileTypes::TYPE_IMAGE) { $tranformationName = 'preview'; $extension = 'jpg'; } } else { $tranformationName = $tranformation->name; $extension = $tranformation->extension; } $commandParams = ['ResponseCacheControl' => 'private, max-age=1200', 'ResponseContentType' => $file->mime_type, 'Bucket' => array_get($this->config, 'bucket'), 'Key' => "{$file->id}/{$tranformationName}.{$extension}"]; if ($download) { $commandParams['ResponseContentDisposition'] = "attachment; filename={$file->filename}"; } $expires = array_get($this->config, 'presigned.expires', '+20 minutes'); $command = $this->client->getCommand('GetObject', $commandParams); return (string) $this->client->createPresignedRequest($command, $expires)->getUri(); }
public function getPreSignedUrl($bucket, $key, $expiryInMinutes = 10) { $this->bucket = $bucket; $this->key = $key; $this->scenario = 'delete'; if ($this->validate()) { try { $s3Client = new S3Client(['version' => 'latest', 'region' => Yii::$app->params['amazon']['region'], 'credentials' => Yii::$app->params['amazon']['credentials']]); $cmd = $s3Client->getCommand('GetObject', ['Bucket' => $this->bucket, 'Key' => $this->key]); $request = $s3Client->createPresignedRequest($cmd, '+' . $expiryInMinutes . ' minutes'); return (string) $request->getUri(); } catch (\Exception $e) { Yii::error('Error getting pre-signed url from S3. Bucket - ' . $this->bucket . ' Key - ' . $this->key . ' Extra - ' . $e->getMessage()); return false; } } return false; }
$o_iter = $s3client->getIterator('ListObjects', array('Bucket' => $s3Bucket)); foreach ($o_iter as $o) { if ($o['Size'] != '0') { // Because we don't want to list folders echo "<form enctype='multipart/form-data' name='fileDELForm' method='post' action='index.php' style='border:1px;' >"; $prefixKey = explode("/", $o['Key']); if ($prefixKey[0] == 'public') { echo "<a href='http://" . $s3Bucket . "/" . $o['Key'] . "'>"; echo "<img src='http://{$s3Bucket}/" . $o['Key'] . "' style='width:100px;height:100px;'/></a>"; echo "<input type='hidden' name='fileKey' id='fileKey' value='" . $o['Key'] . "' />"; echo "{$o['Key']}"; echo "<input type='submit' name='submit' id='submit' value='Delete' />"; } if ($prefixKey[0] == 'QSA') { $objectPre = $s3client->getCommand('GetObject', ['Bucket' => $s3Bucket, 'Key' => $o['Key']]); $presignedRequest = $s3client->createPresignedRequest($objectPre, '+30 minutes'); $objectURL = (string) $presignedRequest->getUri(); echo "<a href='{$objectURL}' >"; echo "<img src='{$objectURL}' style='width:100px;height:100px;' /></a>"; echo "<input type='hidden' name='fileKey' id='fileKey' value='" . $o['Key'] . "' />"; echo "{$o['Key']}"; echo "<input type='submit' name='submit' id='submit' value='Delete' />"; } echo "</form>"; } } // Check if the UL form has been posted if ($submit == 'Upload') { if (isset($s3Bucket) && is_uploaded_file($_FILES['fileUL']['tmp_name'])) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $fileType = finfo_file($finfo, $_FILES['fileUL']['tmp_name']);