我正在尝试以二进制格式循环存储在MS SQL数据库上的一堆图像,并使用php将它们移动到Amazon云上的存储桶中。 我已经尝试了一些方法,但没有成功,而我能够在内存中获取图像文件的实例,这是我无法上传的。阅读了以下帖子:Creating an image without storing it as a local file,我想也许使用Imagick库会更好,但仍然没有运气。
所以我的问题是,我可以使用Undesigned S3类或实际的AWS SDK从读取到PHP内存的图像上传到云端吗?
这是我到目前为止创建的一些代码,以便了解我所采用的方法。我正在使用Undesigned S3类在云上创建对象。图像表上字段上的文件是“IMAGE”类型,包含文件的字符串引用。任何提示,指示,帮助都会很棒。
// get the images that have not been moved to the cloud yet
$obj_File = new Images();
$aImages = $obj_File->Select_ImagesNotOnCloud();
unset($obj_File);
// set the start time
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
// upload the image to the cloud
$images_processed = 0;
// get and include the Amazon sdk
require_once('../includes/classes/library/plugins/amazon/undesigned/S3.php');
$s3 = new S3("key", "secretkey");
$sBucket = 'testbucket';
// loop through each image and move to the cloud
foreach($aImages as $aImage) {
// save the file to the file server with a temporary name
$oImage = imagecreatefromstring($aImage['FileData']);
// generate random filename
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$sMicrotime = $mtime;
$sFilename = date('Ymdhms') . $sMicrotime;
$sFilename = md5(str_replace(array(' ','.'), '', $sFilename));
// get the correct file extension
$sFilename .= '.' . $aImage['FileExtension'];
// Create Imagick object - the new bit I've just started playing with as per https://stackoverflow.com/questions/189368/creating-an-image-without-storing-it-as-a-local-file/189410#189410
$im = new Imagick();
// Convert image into Imagick
$im->readimageblob($image);
// Upload an object from a resource (requires size):
$result = $s3->putObject($s3::inputResource($im->getimageblob(), $im->getSize()), $sBucket, $sFilename, $s3::ACL_PUBLIC_READ);
// try and put the object into the bucket on the cloud
$response = $s3->putObject(
$s3->inputResource($oImage, $aImage['FileSize']),
$sBucket,
$sFilename,
S3::ACL_PUBLIC_READ
);
echo("<pre>"); var_dump($response); echo("</pre>");
echo('<br /><br />File_Id = ' . $aImage['File_Id'] .', filename generated = '. $sFilename .'<br />');
$images_processed++;
// clear the image object
unset($oImage);
}
// get the end time and calculate the seconds it took to run the script
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
// get final time taken to run script
$sTotalTime = ($endtime - $starttime);
$aTime = explode('.', $sTotalTime);
$sTotalTime = $aTime[0] .'.'. substr($aTime[1], 0, 4);
答案 0 :(得分:0)
<强>解强>
如果其他人遇到同样的问题,请为此提供一些解决方案。无法将图像直接从数据库中提取到云端,因此选择获取文件并将其保存到服务器,然后将该文件上传到云端。
我必须使用Imajick库,但下面是代码。
// path to upload the file to the server prior to uploading to the cloud
$sServerUploadFolder = 'D:\Inetpub\wwwroot\site\\tmp\\';
// set parent folder name on the bucket
$sBucketParentFolder = 'images/';
// get the images that have not been moved to the cloud yet
$aImages = GetImages(); // gets an array of image data including the FileData which is in image format from MS SQL database
// set the start time
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
// upload the image to the cloud
$images_processed = 0;
// set the bucket name to upload to
$sBucket = 'testbucket';
// loop through each image and move to the cloud
foreach($aImages as $aImage) {
// generate random filename
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$sMicrotime = $mtime;
$sFilenameToSave = date('Ymdhms') . $sMicrotime;
$sFilenameToSave = md5(str_replace(array(' ','.'), '', $sFilenameToSave));
// get the correct file extension
$sFilenameToSave .= '.' . $aImage['FileExtension'];
// Create Imagick object
$oImagick = new Imagick();
// Convert image into Imagick
$oImagick->readimageblob($aImage['FileData']);
// write the image to the temporary storage folder on the server
$oImagick->writeimage($sServerUploadFolder.$sFilenameToSave);
// upload the file to the cloud
$response = AWSCreateObject($sBucket, $sFilenameToSave, $sServerUploadFolder.$sFilenameToSave, $sBucketParentFolder);
// increment the counter for images processed
$images_processed++;
// delete the image from the temp storage folder
unlink($sServerUploadFolder.$sFilenameToSave);
// clear the image object
unset($oImagick);
}
// get the end time and calculate the seconds it took to run the script
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
// get final time taken to run script
$sTotalTime = ($endtime - $starttime);
$aTime = explode('.', $sTotalTime);
$sTotalTime = $aTime[0] .'.'. substr($aTime[1], 0, 4);
// clear objects
unset($obj_File);
// final output
echo "<h1>images processed = $images_processed, time taken: $sTotalTime seconds</h1>";
/**
* uploads a file to the cloud - should only be used for files that are reasonably small in bucket terms, ie. 10mb or less
* @param string $param_str_bucket name of the bucket to upload to
* @param string $param_str_filename name of the file to be uploaded to the bucket, eg. personphoto.jpg
* @param string $param_str_upload_path full path to the file in the bucket, eg. images/photos/personphoto.jpg
* @param string $param_str_parent_folder optional - name of the parent folder in which to upload the object into
* @param array $param_arr_meta optional - meta data values for the object
* @param boolean $param_bln_public_access optional - declares which acl type to use for the upload, ACL_PUBLIC or ACL_PRIVATE, defaults to ACL_PUBLIC
*/
function AWSCreateObject($param_str_bucket, $param_str_filename, $param_str_upload_path, $param_str_parent_folder = null, $param_arr_meta = null, $param_bln_public_access = 1) {
// load the Amazon class
require_once 'sdk-1.5.7/sdk.class.php';
// Instantiate the Amazon class
$S3 = new AmazonS3();
// set the acl type
$sACL = isset($param_bln_public_access) && $param_bln_public_access == 1 ? AmazonS3::ACL_PUBLIC : AmazonS3::ACL_PRIVATE;
// check the $param_str_parent_folder value
if(isset($param_str_parent_folder)) {
if(substr($param_str_parent_folder, -1) != '/') {
$param_str_parent_folder .= '/';
}
}
// create the object on the cloud
$response = $S3->create_object
(
$param_str_bucket // bucket to upload the file to
, $param_str_parent_folder.$param_str_filename // filename to use for the object to be saved
// Optional configuration
, array(
'fileUpload' => $param_str_upload_path
, 'acl' => $sACL
, 'storage' => AmazonS3::STORAGE_STANDARD
// Object metadata
, 'meta' => $param_arr_meta
)
);
// tidy up
unset($S3);
// return result
}