private function _sendS3($tmpfile, $name) { $config = new Zend_Config_Ini('../application/configs/amazon.ini', 's3'); $s3 = new Zend_Service_Amazon_S3($config->access_key, $config->secret_key); $bytes = file_get_contents($tmpfile); return $s3->putObject($config->imagebucket . "/" . $name . ".jpg", $bytes, array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ)); }
/** * Store an item in the storage service. * * WARNING: This operation overwrites any item that is located at * $destinationPath. * * @TODO Support streams * * @param string $destinationPath * @param string|resource $data * @param array $options * @return void */ public function storeItem($destinationPath, $data, $options = array()) { try { $fullPath = $this->_getFullPath($destinationPath, $options); return $this->_s3->putObject($fullPath, $data, empty($options[self::METADATA]) ? null : $options[self::METADATA]); } catch (Zend_Service_Amazon_S3_Exception $e) { throw new Zend_Cloud_StorageService_Exception('Error on store: ' . $e->getMessage(), $e->getCode(), $e); } }
public function testCommonPrefixes() { $this->_amazon->createBucket($this->_bucket); $this->_amazon->putObject($this->_bucket . '/test-folder/test1', 'test'); $this->_amazon->putObject($this->_bucket . '/test-folder/test2-folder/', ''); $params = array('prefix' => 'test-folder/', 'delimiter' => '/'); $response = $this->_amazon->getObjectsAndPrefixesByBucket($this->_bucket, $params); $this->assertEquals($response['objects'][0], 'test-folder/test1'); $this->assertEquals($response['prefixes'][0], 'test-folder/test2-folder/'); }
/** * Flush current cached stream data to storage * * @return boolean */ public function stream_flush() { // If the stream wasn't opened for writing, just return false if (!$this->_writeBuffer) { return false; } $ret = $this->_s3->putObject($this->_objectName, $this->_objectBuffer); $this->_objectBuffer = null; return $ret; }
/** * @see ZF-7773 */ public function testGetObjectsByBucketParams() { $this->_amazon->createBucket("testgetobjectparams1"); $this->_amazon->putObject("testgetobjectparams1/zftest1", "testdata"); $this->_amazon->putObject("testgetobjectparams1/zftest2", "testdata"); $list = $this->_amazon->getObjectsByBucket("testgetobjectparams1", array('max-keys' => 1)); $this->assertEquals(1, count($list)); $this->_amazon->removeObject("testgetobjectparams1/zftest1", "testdata"); $this->_amazon->removeObject("testgetobjectparams1/zftest2", "testdata"); $this->_amazon->removeBucket("testgetobjectparams1"); }
/** * Creates a new file from data rather than an existing file * * @param Storage_Model_DbRow_File $model The file for operation * @param string $data */ public function write(Storage_Model_File $model, $data) { $path = $this->getScheme()->generate($model->toArray()); // Prefix path with bucket? //$path = $this->_bucket . '/' . $path; // Copy file try { $return = $this->_internalService->putObject($this->_bucket . '/' . $path, $data, array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ, 'Cache-Control' => 'max-age=864000, public')); if (!$return) { throw new Storage_Service_Exception('Unable to write file.'); } } catch (Exception $e) { throw $e; } return $path; }
function s3_put_data($access_key_id, $secret_access_key, $bucket_path, $data, $mime) { $result = FALSE; if ($access_key_id && $secret_access_key && $bucket_path && $data) { try { $s3 = new Zend_Service_Amazon_S3($access_key_id, $secret_access_key); $meta = array(); $meta[Zend_Service_Amazon_S3::S3_ACL_HEADER] = Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ; if ($mime) { $meta[Zend_Service_Amazon_S3::S3_CONTENT_TYPE_HEADER] = $mime; } if ($s3->putObject($bucket_path, $data, $meta)) { $result = TRUE; } else { print 'putObject failed'; } } catch (Exception $ex) { print $ex->getMessage(); } } return $result; }
/** * @param string $filename * @param string $data Binary file data * @param boolean $overwrite Whether to overwrite this file, or create a unique name * @param boolean $formatFilename Whether to correct the filename, * f.i. ensuring lowercase characters. * @return string Destination filename. */ public function store($filename, $data, $overwrite = false, $formatFilename = true) { $this->_initApi(); $this->_createBucketIfNecessary(); if ($formatFilename) { $filename = Garp_File::formatFilename($filename); } if (!$overwrite) { while ($this->exists($filename)) { $filename = Garp_File::getCumulativeFilename($filename); } } $path = $this->_config['bucket'] . $this->_getUri($filename); $meta = array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ); if (false !== strpos($filename, '.')) { $ext = substr($filename, strrpos($filename, '.') + 1); if (array_key_exists($ext, $this->_knownMimeTypes)) { $mime = $this->_knownMimeTypes[$ext]; } else { $finfo = new finfo(FILEINFO_MIME); $mime = $finfo->buffer($data); } } else { $finfo = new finfo(FILEINFO_MIME); $mime = $finfo->buffer($data); } $meta[Zend_Service_Amazon_S3::S3_CONTENT_TYPE_HEADER] = $mime; if ($this->_config['gzip'] && $this->_gzipIsAllowedForFilename($filename)) { $meta['Content-Encoding'] = 'gzip'; $data = gzencode($data); } if ($this->_api->putObject($path, $data, $meta)) { return $filename; } return false; }
/** !! ************************************************************************ * The following methods take the request data, validate it, parse it, * and store it if there were no previous errors. ************************************************************************ */ public function basicProfileSave($request_data) { global $error_msg; $thumb = null; $file_name = null; $file_mime_type = null; $file_size = null; $apiDataArray = array('person' => null); $this->isError = TRUE; if (empty($request_data['first_name'])) { $this->message = __('Fields marked with * can not be empty, First name can not be empty.'); } else { if (!empty($request_data['pass']) || !empty($request_data['conpass'])) { $set_new_password = true; $new_password_ok = false; if ($request_data['pass'] != $request_data['conpass']) { $this->message = __('Password and confirm password should match.'); } else { if (strlen($request_data['pass']) < PA::$password_min_length) { $this->message = sprintf(__('Password should be of %s characters or more.'), PA::$password_min_length); } else { if (strlen($request_data['pass']) > PA::$password_max_length) { $this->message = sprintf(__('Password should be less than %s charcaters.'), PA::$password_max_length); } else { // all is good $new_password_ok = true; } } } } } if (isset($request_data['postal_code']) && isset($request_data['postal_code']['value']) && !empty($request_data['postal_code']['value'])) { $zipCode = trim($request_data['postal_code']['value']); $validator = new Zend_Validate_PostCode("en_US"); if (!$validator->isValid($zipCode)) { $this->message = "The zip code is invalid."; } else { $request_data['postal_code']['value'] = $zipCode; $apiDataArray['person']['zip_code'] = $zipCode; } } if (isset($request_data['about']) && isset($request_data['about']['value']) && !empty($request_data['about']['value'])) { $about = trim($request_data['about']['value']); $validator = new Zend_Validate_StringLength(array('max' => 500)); if (!$validator->isValid($about)) { $this->message = "The about field is limited to 500 characters. There are " . strlen($about) . " characters in the about field"; } } if ($request_data['deletepicture'] == "true") { $this->handleDeleteUserPic($request_data); } /* Parag Jagdale - 10/27/2010: Upload files with Zend library, Resize files and upload to AmazonS3 */ if (empty($this->message) && !empty($_FILES['userfile']['name'])) { $file = null; // Recieve uploaded file $zendUploadAdapter = new Zend_File_Transfer_Adapter_Http(); $zendUploadAdapter->setDestination(PA::$upload_path); $zendUploadAdapter->addValidator('Size', false, '1MB')->addValidator('Count', false, 1)->addValidator('FilesSize', false, array('min' => '1kB', 'max' => '2MB'))->addValidator('Extension', false, array('jpeg', 'jpg', 'png', 'gif'))->addValidator('ImageSize', false, array('minwidth' => 40, 'maxwidth' => 1024, 'minheight' => 40, 'maxheight' => 768)); if (!$zendUploadAdapter->isValid()) { // Invalid image, so show errors $this->message = __("The image you selected as your photo has some errors: <br />" . implode("<br />", $zendUploadAdapter->getMessages())); } try { $zendUploadAdapter->receive(); } catch (Zend_File_Transfer_Exception $e) { $this->message = $e->getMessage(); } if (empty($this->message)) { //If there is no error message then try saving to amazon s3 // save images to amazon s3 global $app; $aws_key = null; $aws_secret_key = null; $aws_key = $app->configData['configuration']['api_keys']['value']['amazon_aws_key']['value']; $aws_secret_key = $app->configData['configuration']['api_keys']['value']['amazon_aws_secret']['value']; $amazon_bucket_name = $app->configData['configuration']['api_keys']['value']['amazon_s3_bucket']['value']; if (isset($aws_key) && isset($aws_secret_key)) { $s3 = null; $bucketAvailable = false; $bucketCreated = false; $s3 = new Zend_Service_Amazon_S3($aws_key, $aws_secret_key); if (isset($s3)) { $bucketCreated = false; $bucketAvailable = $s3->isBucketAvailable($amazon_bucket_name); if (!$bucketAvailable) { if ($s3->createBucket($amazon_bucket_name)) { // new bucket was created $bucketCreated = true; } } else { $bucketCreated = true; } if ($bucketCreated == true) { // delete old amazon s3 pictures $this->handleDeleteUserPic($request_data); $file_path = $zendUploadAdapter->getFileName('userfile', true); $file_name = $zendUploadAdapter->getFileName('userfile', false); $file_size = $zendUploadAdapter->getFileSize('userfile'); $file_name = $this->cleanupFileName($file_name); $file_info = getimagesize($file_path); $file_mime_type = isset($file_info) && isset($file_info['mime']) && !empty($file_info['mime']) ? $file_info['mime'] : null; $apiDataArray['person'] = array('avatar' => array('file_name' => $file_name, 'content_type' => $file_mime_type, 'file_size' => $file_size)); // api_call needs to be set to true in order for the User class to update the avatar and avatar_small fields $this->user_info->api_call = true; foreach ($this->_image_sizes as $imageSizeType => $imageDimensions) { // Resize image into thumbnails $imageAsString = null; try { $thumb = PhpThumbFactory::create($file_path); if (!isset($this->user_info->core_id) && !empty($this->user_info->core_id)) { $this->user_info->core_id = 0; } $objectPath = $this->buildAmazonS3ObjectURL($imageSizeType, $this->user_info->core_id, $file_name); if (isset($imageDimensions) && !empty($imageDimensions)) { // if this is an original size image, the width and height dont need to be set $thumb->adaptiveResize($imageDimensions['width'], $imageDimensions['height']); } $imageAsString = $thumb->getImageAsString(); $amazonURL = "http://s3.amazonaws.com/" . $amazon_bucket_name; switch ($imageSizeType) { case "large": $this->user_info->picture = $objectPath; $this->user_info->picture_dimensions = $imageDimensions; break; case "standard": $this->user_info->avatar = $objectPath; $this->user_info->avatar_dimensions = $imageDimensions; break; case "medium": $this->user_info->avatar_small = $objectPath; $this->user_info->avatar_small_dimensions = $imageDimensions; break; default: break; } } catch (Exception $e) { $this->message = $e->getMessage(); break; } if (isset($imageAsString) && !empty($imageAsString)) { // send object to AmazonS3 $s3->putObject($amazon_bucket_name . $objectPath, $imageAsString, array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ)); } } } } } } } if (empty($this->message)) { // Add first name and last name if they have been changed to the api update data array if ($this->user_info->first_name != $request_data['first_name'] || $this->user_info->last_name != $request_data['last_name']) { $apiDataArray['person']['name'] = $request_data['first_name'] . ' ' . $request_data['last_name']; } //If there is no error message then try saving the user information. $this->user_info->first_name = $request_data['first_name']; $this->user_info->last_name = $request_data['last_name']; try { if (!empty($request_data['pass'])) { $passwordSaltArray = $this->EncryptPassword($request_data['pass']); if (isset($passwordSaltArray)) { list($encryptedPassword, $salt) = $passwordSaltArray; $this->user_info->password = $encryptedPassword; $apiDataArray['person']['encrypted_password'] = $encryptedPassword; // remove last $ from salt because ruby doesn't like it $salt = rtrim($salt, '$'); $apiDataArray['person']['password_salt'] = $salt; } else { $this->message = "Your password could not be changed, please contact the administrator."; } } } catch (Exception $e) { $this->message = $e->getMessage(); } } if (empty($this->message)) { try { $this->user_info->save(); $dynProf = new DynamicProfile($this->user_info); $dynProf->processPOST('basic'); $dynProf->save('basic', GENERAL); $this->message = __('Profile updated successfully.'); // $this->redirect2 = PA_ROUTE_EDIT_PROFILE; // $this->queryString = '?type='.$this->profile_type; //TODO: change URL after the contributions activity stream URLs have been changed $url = CC_APPLICATION_URL . CC_APPLICATION_URL_TO_API . $this->user_info->user_id; // Try to send updated data to Core (Ruby) $this->sendUserDataToCivicCommons($url, $apiDataArray); $this->isError = FALSE; } catch (PAException $e) { $this->message = $e->message; } } $error_msg = $this->message; }
$dumps[] = $file_name; } } if ($wp_main_tables = $db->fetchCol('SHOW TABLES FROM `' . WORDPRESS_MULTISITE_DB_NAME . '` WHERE Tables_in_' . WORDPRESS_MULTISITE_DB_NAME . ' REGEXP "' . $wp_table_prefix . '_[a-z]"')) { $file_name = 'wordpress.sql' . (GZIP_DUMP_FILES ? '.gz' : ''); printLog('Dumping database tables for the wordpress core'); mysqldump(WORDPRESS_MULTISITE_DB_NAME, $file_name, $wp_main_tables); $dumps[] = $file_name; } } if (UPLOAD_TO_AWS) { $s3 = new Zend_Service_Amazon_S3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY); foreach ($dumps as $dump) { $file_contents = file_get_contents(DUMPS_PATH . '/' . $dump); printLog('Uploading ' . $dump . ' to the Amazon S3 storage'); $upload_succesful = $s3->putObject(AWS_BUCKET_NAME . '/' . $timestamp . '/' . $dump, $file_contents); if (DELETE_AFTER_UPLOAD && $upload_succesful === true) { printLog('Deleting ' . $dump . ' from the local file system'); unlink(DUMPS_PATH . '/' . $dump); } } if (DELETE_AFTER_UPLOAD) { rmdir(DUMPS_PATH); } } } catch (Exception $e) { printLog($e->getMessage()); } function mysqldump($db_name, $file_name, $tables = null) { $mysqldump = sprintf('%s "%s" -u %s --password=%s', MYSQLDUMP, $db_name, DB_USERNAME, DB_PASSWORD);
/** * Move the dump file to a Amazon S3 bucket * * @param string $filename * @return void */ protected function moveDumpToS3($filename) { $s3Config = $this->config->s3; $this->validateS3Settings($s3Config); $s3File = $s3Config->aws_bucket . '/' . $filename; $this->writer->line('Copy ' . $filename . ' -> Amazon S3: ' . $s3File); $s3 = new Zend_Service_Amazon_S3($s3Config->aws_key, $s3Config->aws_secret_key); // use https for uploading $s3->setEndpoint('https://' . Zend_Service_Amazon_S3::S3_ENDPOINT); $s3->putObject($s3Config->aws_bucket . '/' . $filename, file_get_contents($filename)); $s3->getObject($s3Config->aws_bucket . '/' . $filename); }