Example #1
0
 public function consolidate($packageCode, $user, $name = '')
 {
     $processedFiles = [];
     $temporalPackage = unserialize($this->redis->get($packageCode));
     $files = $temporalPackage->getFiles();
     // Create Package
     $package = new Package();
     $package->setCode($packageCode)->setCreatedAt(new \DateTime())->setSize(0)->setUser($user)->setName($name)->setFrozen(Package::NOT_FROZEN);
     // Calculate Short Code
     $i = 0;
     do {
         $shortCode = substr($packageCode, $i, 6);
     } while ($this->doctrine->getManager()->getRepository('AppBundle:Package')->shortCodeExists($shortCode));
     $package->setShortCode($shortCode);
     foreach ($files as $temporalFile) {
         $file = new File();
         $file->setSize(filesize($this->temporalDir . '/' . $packageCode . '-' . $temporalFile->getHash() . '.obj'))->setHash($temporalFile->getHash())->setName($temporalFile->getName())->setPackage($package);
         $package->setSize($package->getSize() + $file->getSize());
         try {
             $uploader = new MultipartUploader($this->s3Client, $this->temporalDir . '/' . $packageCode . '-' . $temporalFile->getHash() . '.obj', ['bucket' => $this->bucket, 'key' => $file->getUri()]);
             $uploader->upload();
             $processedFiles[$temporalFile->getName()] = true;
             $this->doctrine->getManager()->persist($file);
             unlink($this->temporalDir . '/' . $packageCode . '-' . $temporalFile->getHash() . '.obj');
         } catch (MultipartUploadException $e) {
             $processedFiles[$temporalFile->getName()] = $e->getMessage();
         }
     }
     $this->doctrine->getManager()->persist($package);
     $this->doctrine->getManager()->flush();
     $this->redis->del($packageCode);
     return $processedFiles;
 }
 /**
  * @param $filePath
  *
  * @return string
  */
 public function upload($filePath)
 {
     $extension = pathinfo($filePath, PATHINFO_EXTENSION);
     $fileName = sprintf('%d-%s.%s', time(), uniqid(), $extension);
     $uploader = new MultipartUploader($this->client, $filePath, ['bucket' => $this->bucketName, 'key' => $fileName]);
     $result = $uploader->upload();
     return $result['ObjectURL'];
 }
 /**
  * @param File $f
  * @throws Exception
  */
 public function put(File $f)
 {
     $fp = fopen($f->getFullPath(), 'r');
     if (!$fp) {
         throw new Exception('Unable to open file: ' . $f->getFilename());
     }
     $uploader = new MultipartUploader($this->client, $f->getFullPath(), array('bucket' => $this->containerName, 'key' => $this->getRelativeLinkFor($f)));
     try {
         $uploader->upload();
     } catch (MultipartUploadException $e) {
         // warning: below exception gets silently swallowed!
         error_log('S3Bucket: failed to put file: ' . $e->getPrevious()->getMessage());
         throw new Exception('S3Bucket: failed to put file: ' . $e->getPrevious()->getMessage(), 0, $e);
     }
 }
Example #4
0
 /**
  * In case of large files, use the multipartUpload method, so the file is sent in chunks to S3, without the need
  * to read the complete file in memory.
  *
  * @param     $bucket
  * @param     $key
  * @param     $sourcePath
  * @param int $concurrency
  *
  * @return mixed
  */
 public function multipartUpload($bucket, $key, $sourcePath, $concurrency = 1)
 {
     $uploader = new MultipartUploader($this->instance, $sourcePath, ['bucket' => $bucket, 'key' => $key]);
     try {
         $uploader->upload();
         return true;
     } catch (MultipartUploadException $e) {
         return false;
     }
 }
 /**
  * send file to aws bucket
  * @param $absolutePath String locale file
  * @param $awsTargetPath String  aws targetPath
  * @return \Aws\Result
  * @throws \NotFoundBucket
  */
 private function multipartUploader($absolutePath, $awsTargetPath)
 {
     $bucket = 'my_bucket_name';
     $uploader = new MultipartUploader($this->s3, $absoluteFilePath, ['bucket' => $absolutePath, 'key' => $awsTargetPath]);
     do {
         try {
             $result = $uploader->upload();
         } catch (MultipartUploadException $e) {
             $uploader = new MultipartUploader($this->s3, $absoluteFilePath, ['state' => $e->getState()]);
         }
     } while (!isset($result));
     try {
         $result = $uploader->upload();
         return $result;
     } catch (MultipartUploadException $e) {
         throw $e;
     }
 }
 /**
  *  Uploads the objects file and thumbnails, if they exist, to Amazon S3 from the temporary storage location.
  *  Does not delete any temporary files, call deleteFromTemporary() for this.
  *
  *  Preferentially uses Amazon's Multipart Upload functionality when the file size exceeds 100MB.
  */
 public function putToCloud()
 {
     $s3 = AWS::createClient('s3');
     if ($this->hasFile()) {
         if ($this->exceedsMultipartUploadThreshold()) {
             $uploader = new MultipartUploader($s3, public_path($this->media), ['Bucket' => Config::get('filesystems.disks.s3.bucket'), 'Key' => $this->filename, 'ACL' => $this->visibility === VisibilityStatus::PublicStatus ? 'public-read' : 'private']);
             try {
                 $uploader->upload();
             } catch (MultipartUploadException $e) {
                 Log::error('Item was not uploaded', ['message' => $e->getMessage()]);
             }
         } else {
             $s3->putObject(['Bucket' => Config::get('filesystems.disks.s3.bucket'), 'Key' => $this->filename, 'Body' => file_get_contents(public_path($this->media)), 'ACL' => $this->visibility === VisibilityStatus::PublicStatus ? 'public-read' : 'private']);
         }
         $this->has_cloud_file = true;
     }
     if ($this->hasThumbs()) {
         $s3->putObject(['Bucket' => Config::get('filesystems.disks.s3.bucketLargeThumbs'), 'Key' => $this->thumb_filename, 'Body' => file_get_contents(public_path($this->media_thumb_large)), 'ACL' => $this->visibility === VisibilityStatus::PublicStatus ? 'public-read' : 'private', 'StorageClass' => 'REDUCED_REDUNDANCY']);
         $s3->putObject(['Bucket' => Config::get('filesystems.disks.s3.bucketSmallThumbs'), 'Key' => $this->thumb_filename, 'Body' => file_get_contents(public_path($this->media_thumb_small)), 'ACL' => 'public-read', 'StorageClass' => 'REDUCED_REDUNDANCY']);
         $this->has_cloud_thumbs = true;
     }
     $this->reindex();
     return $this;
 }
Example #7
0
 /**
  * @param string $filename
  * @param mixed $source
  * @param int $concurrency
  * @param int $partSize
  * @param string $acl
  * @param array $options
  *
  * @return \Aws\ResultInterface
  */
 public function multipartUpload($filename, $source, $concurrency = null, $partSize = null, $acl = null, array $options = [])
 {
     $args = $this->prepareArgs($options, ['bucket' => $this->bucket, 'acl' => !empty($acl) ? $acl : $this->defaultAcl, 'key' => $filename, 'concurrency' => $concurrency, 'part-size' => $partSize]);
     $uploader = new MultipartUploader($this->getClient(), $source, $args);
     return $uploader->upload();
 }
Example #8
0
 public function upload()
 {
     $upload_path_url = 'upload';
     if (!is_dir($upload_path_url)) {
         @mkdir($upload_path_url);
     }
     $config['upload_path'] = $upload_path_url . '/';
     $config['allowed_types'] = 'jpg|jpeg|png|gif|bmp';
     $config['encrypt_name'] = TRUE;
     $config['max_size'] = 1024 * 1024 * 10;
     // Bytes, 1024 * 1024 * 10 = 10MB
     $this->_CI->load->library('upload', $config);
     if (!$this->_CI->upload->do_upload('img')) {
         log_message('error', 'upload fail');
         return FALSE;
         // @TODO Implement for upload error
         // $meta = $this->_CI->upload->data();
         // $info = new StdClass;
         // $info->name = $meta['file_name'];
         // $info->size = $meta['file_size'];
         // $info->type = $meta['file_type'];
         // $info->error = $this->upload->display_errors('', '');
         // $files[] = $info;
         //
         // $res['result'] = FALSE;
         // $res['files'] = $files;
         // $this->output->set_content_type('application/json')->set_output(json_encode($res));
     } else {
         log_message('debug', 'Detect an image');
         $meta = $this->_CI->upload->data();
         // make thumbnail
         $config = array();
         $config['image_library'] = 'gd2';
         $config['source_image'] = $meta['full_path'];
         $config['create_thumb'] = TRUE;
         $config['maintain_ratio'] = TRUE;
         $config['width'] = 75;
         $config['height'] = 50;
         $this->_CI->load->library('image_lib', $config);
         $this->_CI->image_lib->resize();
         //print_r($_SERVER['DOCUMENT_ROOT']);
         //print_r($meta);
         //return;
         $thumb['full_name'] = $meta['raw_name'] . '_thumb' . $meta['file_ext'];
         $thumb['full_path'] = $meta['file_path'] . $thumb['full_name'];
         //include_once $_SERVER['DOCUMENT_ROOT']."/aws-sdk-php/vendor/autoload.php";
         $s3 = new Aws\S3\S3Client(['version' => 'latest', 'region' => self::AWS_REGION, 'credentials' => ['key' => self::AWS_KEY, 'secret' => self::AWS_SECRET_KEY]]);
         $uploader = new MultipartUploader($s3, $thumb['full_path'], ['bucket' => 's3cs', 'key' => 'shop_img/' . $thumb['full_name']]);
         try {
             $result = $uploader->upload();
             //echo "Upload complete: {$result['ObjectURL'}\n";
         } catch (MultipartUploadException $e) {
             //echo $e->getMessage() . "\n";
             log_message('error', $e);
             return FALSE;
         }
         //use Aws\Common\Aws;
         // Instantiate an S3 client
         /*
                 // Upload a publicly accessible file. File size, file type, and md5 hash are automatically calculated by the SDK
                 $result = $s3->putObject(array(
                     'Bucket' => 's3cs/shop_img/',
                     'Key'    => $thumb['full_name'],
                     'Body'   => fopen($thumb['full_path'], 'r'),
                     //'ACL'    => Aws\S3\Enum\CannedAcl::PUBLIC_READ,
                     'ContentType'=>mime_content_type($thumb['full_path'])
                 ));
               } catch(S3Exception $e){
                 log_message('error', $e);
                 return FALSE;
               }
         */
         return $result['ObjectURL'];
     }
 }