[英]Integrating JqueryUpload plugin with AWS php sdk to upload images to S3
我正在將jqueryfileupload插件與AWS集成。我已經成功完成了上傳部分,但是現在我希望集成圖像調整大小功能。
我正在使用此插件代碼。我已經建立了一個使用最小代碼的示例,如下所示。
的index.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>jQuery File Upload Example</title>
</head>
<body>
<input id="fileupload" type="file" name="files[]" data-url="aws/" multiple>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/vendor/jquery.ui.widget.js"></script>
<script src="js/jquery.iframe-transport.js"></script>
<script src="js/jquery.fileupload.js"></script>
<script>
$(function () {
$('#fileupload').fileupload({
dataType: 'json',
done: function (e, data) {
$.each(data.result.files, function (index, file) {
$('<p/>').text(file.name).appendTo(document.body);
});
}
});
});
</script>
</body>
</html>
awssdk.php ---這是我在選擇圖像后調用的文件。
<?php
$bucket = "my bucket name";
$subFolder = ""; // leave blank for upload into the bucket directly
if (!class_exists('S3'))require_once('S3.php');
//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'my key');
if (!defined('awsSecretKey')) define('awsSecretKey', 'my secret key');
$options = array( 'image_versions' => array(
'small' => array(
'max_width' => 1920,
'max_height' => 1200,
'jpeg_quality' => 95
),
'medium' => array(
'max_width' => 800,
'max_height' => 600,
'jpeg_quality' => 80
),
'thumbnail' => array(
'max_width' => 80,
'max_height' => 80
)
)
);
//instantiate the class
$s3 = new S3(awsAccessKey, awsSecretKey);
function getFileInfo($bucket, $fileName) {
global $s3;
$fileArray = "";
$size = $s3->getBucket($bucket);
$furl = "http://" . $bucket . ".s3.amazonaws.com/".$fileName;
$fileArray['name'] = $fileName;
$fileArray['size'] = $size;
$fileArray['url'] = $furl;
$fileArray['thumbnail'] = $furl;
$fileArray['delete_url'] = "server/php/index.php?file=".$fileName;
$fileArray['delete_type'] = "DELETE";
return $fileArray;
}
function uploadFiles($bucket, $prefix="") {
global $s3;
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
return "";
}
$upload = isset($_FILES['files']) ? $_FILES['files'] : null;
$info = array();
if ($upload && is_array($upload['tmp_name'])) {
foreach($upload['tmp_name'] as $index => $value) {
$fileTempName = $upload['tmp_name'][$index];
$fileName = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index]);
$fileName = $prefix.str_replace(" ", "_", $fileName);
// $response = $s3->create_object($bucket, $fileName, array('fileUpload' => $fileTempName, 'acl' => AmazonS3::ACL_PUBLIC, 'meta' => array('keywords' => 'example, test'),));
$response = $s3->putObjectFile($fileTempName,$bucket,'images/'.$fileName,S3::ACL_PUBLIC_READ);
//print_r($response);
if ($response==1) {
$info[] = getFileInfo($bucket, $fileName);
} else {
echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
}
}
} else {
if ($upload || isset($_SERVER['HTTP_X_FILE_NAME'])) {
$fileTempName = $upload['tmp_name'];
$fileName = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : $upload['name']);
$fileName = $prefix.str_replace(" ", "_", $fileName);
//$response = $s3->create_object($bucket, $fileName, array('fileUpload' => $fileTempName, 'acl' => AmazonS3::ACL_PUBLIC, 'meta' => array('keywords' => 'example, test'),));
$response = $s3->putObjectFile($upload['tmp_name'],$bucket,$fileName,S3::ACL_PUBLIC_READ);
if ($response->isOK()) {
$info[] = getFileInfo($bucket, $fileName);
} else {
echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
}
}
}
header('Vary: Accept');
$json = json_encode($info);
$redirect = isset($_REQUEST['redirect']) ? stripslashes($_REQUEST['redirect']) : null;
if ($redirect) {
header('Location: ' . sprintf($redirect, rawurlencode($json)));
return;
}
if (isset($_SERVER['HTTP_ACCEPT']) && (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) {
header('Content-type: application/json');
} else {
header('Content-type: text/plain');
}
return $info;
}
?>
這是我正在使用的S3類。
JqueryUploadPLugin附帶了一個服務器端PHP類來上傳圖像,這很棒,但是當我使用AWS時,我必須使用它們的API來上傳圖像,並且插件代碼不起作用。如上所述,我已經實現了上傳部分,但是在我上傳之前需要幫助來創建圖像縮略圖和其他尺寸的圖像。即,我希望以3種格式上傳圖像, ex: original,1024x768,100x100
。
UploadHandler.php有幾個用於創建縮放圖像的protected function create_scaled_image($file_name, $version, $options)
示例: protected function create_scaled_image($file_name, $version, $options)
用於縮放等。 由於我是OO php和AWS的新手,所以我一直堅持集成這些功能。
任何人做類似的事情並可以提供意見將是有幫助的
謝謝
看來您正在嘗試在awssdk.php中的代碼內使用UploadHandler類中的方法。
我認為正確的方法是自定義UploadHandler類-更具體地說是handle_file_upload
函數。 當您以這種方式訪問UploadHandler類的所有良好功能時,這可能對您更有利。
您可以將以下幾行放在awssdk.php中
require('UploadHandler.php');
$upload_handler = new UploadHandler();
您可以看到當前該函數中的代碼將上傳的文件存儲在“ upload_dir”選項中設置的路徑中。 您只需要在該函數內創建S3
類的對象,然后更改代碼以將上傳的文件存儲到S3。
我認為您必須在UploadHandler中更改的行可能是703行。
move_uploaded_file($uploaded_file, $file_path);
應該變成:
$s3 = new S3(awsAccessKey, awsSecretKey);
$response = $s3->putObjectFile($uploaded_file,$bucket,$file->name,S3::ACL_PUBLIC_READ);
if ($response->isOK()) {
$info[] = getFileInfo($bucket, $fileName);
} else {
$file->error = "<strong>Something went wrong while uploading your file... sorry.</strong>";
}
您可能還需要將相關代碼(例如getFileInfo函數)帶入UploadHandler類。
第707-711行似乎用於處理通過PUT方法上傳的文件。 要處理這種情況,您必須保留這些行,以便首先將文件上傳到服務器,然后再將文件傳輸到S3,然后可以取消鏈接服務器中的文件。 但是,如果僅允許使用POST方法,則僅注釋掉這些行也是安全的。
697-701行似乎是用於處理拆分上傳的文件(我不確定)。 如果您也想處理這種情況,則也必須更改它。
也許您正在尋找的是Stream Wrapper。 http://docs.aws.amazon.com/aws-sdk-php/guide/latest/service-s3.html#amazon-s3-stream-wrapper
"The Amazon S3 stream wrapper allows you to store and retrieve data from Amazon S3 using built-in PHP functions like file_get_contents, fopen, copy, rename, unlink, mkdir, rmdir, etc."
我也在尋找相同的解決方案。 我已經建立了這個https://gist.github.com/tim-peterson/8172999,對我有幫助。 我仍在等待AWS批准我的帳戶,因此無法測試任何解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.