/** * Загружает файл в S3. */ public static function moveFileToS3($fileName, $mimeType = null, $baseName = null) { self::checkEnv($ctx = Context::last()); $conf = $ctx->config->get('modules/s3'); $s3 = new S3($conf['accesskey'], $conf['secretkey']); if (!($bucketName = trim($ctx->config->get('modules/s3/bucket', 'files'), '/'))) { throw new RuntimeException(t('Модуль s3 не настроен (bucket).')); } if ($folderName = $ctx->config->get('module/s3/folder', 'files')) { $folderName .= '/'; } /* if (!in_array($bucketName, $s3->listBuckets())) throw new RuntimeException(t('Нет такой папки: ' . $bucketName)); */ if ($f = fopen($fileName, 'rb')) { if (null === $baseName) { $baseName = basename($fileName); } if (!($r = S3::inputResource($f, filesize($fileName)))) { throw new RuntimeException(t('Не удалось создать ресурс из файла %filename.', array('%filename' => $fileName))); } if (!($response = S3::putObject($r, $bucketName, $folderName . $baseName, S3::ACL_PUBLIC_READ))) { throw new RuntimeException(t('Не удалось загрузить файл %filename в папку %bucket.', array('%filename' => $fileName, '%bucket' => $bucketName))); } $url = 'http://' . $bucketName . '.s3.amazonaws.com/' . $folderName . $baseName; Logger::log('S3: ' . $url); return $url; } }
static function createSquareFile($filename, $readPath, $writePath, $writeSize = 300, $upload = true) { # make sure all inputs are clean if (!self::areClean(array($filename, $readPath, $writePath, $writeSize))) { return false; } if (!self::imageMagickInstalled()) { return false; } if (!($size = getimagesize($readPath))) { return false; } $savePath = sfConfig::get('sf_image_dir') . DIRECTORY_SEPARATOR . $writePath . DIRECTORY_SEPARATOR . $filename; if ($size[0] > $size[1] * 2 || $size[1] > $size[0] * 2) { # pad to square if one dimension is more than twice the other dimension exec(sfConfig::get('app_imagemagick_binary_path') . " {$readPath} -virtual-pixel background -background white -set option:distort:viewport \"%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:max((h-w)/2,0)]-%[fx:max((w-h)/2,0)]\" -filter point -distort SRT 0 +repage {$savePath}"); } else { # otherwise, crop to square exec(sfConfig::get('app_imagemagick_binary_path') . " {$readPath} -virtual-pixel edge -set option:distort:viewport \"%[fx:min(w,h)]x%[fx:min(w,h)]+%[fx:max((w-h)/2,0)]+%[fx:max((h-w)/2,0)]\" -filter point -distort SRT 0 +repage {$savePath}"); } # resize exec(sfConfig::get('app_imagemagick_binary_path') . " {$savePath} -resize {$writeSize}x{$writeSize} {$savePath}"); # if s3 enabled, save to s3 if ($upload && sfConfig::get('app_amazon_enable_s3')) { $s3 = new S3(sfConfig::get('app_amazon_access_key'), sfConfig::get('app_amazon_secret_key')); $input = $s3->inputResource($f = fopen($savePath, "rb"), $s = filesize($savePath)); $uri = self::generateS3path($writePath, $filename); if (!S3::putObject($input, sfConfig::get('app_amazon_s3_bucket'), $uri, S3::ACL_PUBLIC_READ)) { return false; } } return $savePath; }
/** * Uploads a file to S3 storage bucket *. * @param $media_file_or_id entity or integer * @param $filepath string * @param $custom_filename string (optional name of file to be stored) * @param $media_work entity * @param $extra_dir string * @return mixed location of where it was stored or false. (ex: 'https://s3.amazonaws.com/BUCKET_NAME/...rest of path' */ public static function store_media($media_file_or_id, $filepath, $custom_filename = '', $media_work = null, $extra_dir = '') { $client = self::_get_client(); if ($client) { if (!$custom_filename) { $filename = basename($filepath); } else { $filename = $custom_filename; } if ($storage_path = parent::get_path($media_file_or_id, $filename, $media_work, $extra_dir)) { // specify the custom filename instead of the one generated by get_path() if ($custom_filename) { $storage_path = str_replace(basename($storage_path), $custom_filename, $storage_path); } if (strpos($filepath, "http") === 0) { return S3_BASE_URL . S3_BUCKET_NAME . '/' . $storage_path; } else { $fp = fopen($filepath, 'r'); $filesize = filesize($filepath); $input = S3::inputResource($fp, $filesize); if ($client->putObject($input, S3_BUCKET_NAME, $storage_path, S3::ACL_PUBLIC_READ, array(), self::_get_content_type($media_file_or_id, $filename))) { return S3_BASE_URL . S3_BUCKET_NAME . '/' . $storage_path; } else { trigger_error('Upload to S3 failed.'); } } } else { trigger_error('An invalid storage path was created.'); } } else { trigger_error('Could not create client object for interacting with S3 service.'); } return false; }
/** * Method to upload file to S3. * This method also deletes the old files from S3. * * @param object $model Object of current model * @return boolean */ function __uploadToS3(&$model) { App::import('Vendor', 'S3', array('file' => 'S3.php')); // Run a loop on all files to be uploaded to S3 foreach ($this->files as $field => $file) { $accessKey = $this->__accessKey; $secretKey = $this->__secretKey; // If we have S3 credentials for this field/file if (!empty($this->settings[$model->name][$field]['s3_access_key']) && !empty($this->settings[$model->name][$field]['s3_secret_key'])) { $accessKey = $this->settings[$model->name][$field]['s3_access_key']; $secretKey = $this->settings[$model->name][$field]['s3_secret_key']; } // Instantiate the class $aws = new S3($accessKey, $secretKey); // If there is an old file to be removed if (!empty($file['old_filename'])) { $aws->deleteObject($this->settings[$model->name][$field]['s3_bucket'], $file['old_filename']); } // Put the object on S3 $isUploaded = $aws->putObject($aws->inputResource(fopen($file['tmp_name'], 'rb'), filesize($file['tmp_name'])), $this->settings[$model->name][$field]['s3_bucket'], $file['name'], $this->settings[$model->name][$field]['s3_acl'], $this->settings[$model->name][$field]['s3_meta_headers'], $this->settings[$model->name][$field]['s3_request_headers']); // If S3 upload failed then set the model error if ($isUploaded == false) { $model->invalidate($this->settings[$model->name][$field]['formfield'], 's3_upload_error'); return false; } // Set the field values to be saved in table $model->data[$model->name][$field] = $file['name']; } return true; }
public static function putObjectFile1($file, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $contentType = null) { return self::putObject(S3::inputResource(fopen($file, "rb"), filesize($file)), $bucket, $uri, $acl); }