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 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?