dongzinen1061 2013-04-15 09:38
浏览 26
已采纳

将从数据库读取的图像上载到Amazon云

I'm trying to loop through a bunch of images that are stored on an MS SQL database in binary format and moving them to the a bucket on the Amazon cloud using php. I've tried a few methods but with no success, whereas I'm able to grab an instance of the image file in memory, it's this that I can't get uploaded. Having read the following post: Creating an image without storing it as a local file, I thought maybe using the Imagick library would be better but still no luck.

So my question is, can I upload from images read into PHP's memory to the cloud either using the Undesigned S3 class or the actual AWS SDK?

Here's some code that I've created so far to get where I am so to see the approach I've taken. I'm using the Undesigned S3 class for object creation on cloud. The file on the field on the image table is of type 'IMAGE', containing a string reference of the file. Any tips, pointers, help would be great.

// 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); 
  • 写回答

1条回答 默认 最新

  • dongtan5558 2013-04-16 07:27
    关注

    SOLUTION

    Have a bit of a solution for this should anyone else ever have the same issue. Couldn't get the image directly from the database to the cloud in the end, so opted to get the file and save it to the server and then upload that file to the cloud.

    I've had to go with the Imajick library though for this, code below.

    // 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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊