我试图克服与S3有关的路障情况。
背景:从我的移动应用程序中,用户最多可以选择10张图像并将其发布到服务器。它被接收并移到AWS实例中的相关文件夹中-为了便于讨论,我们将其称为实例“ A”。当文件移动到实例“ A”时,我们称为“ moveFileToS3”。代码看起来像这样。
foreach($_FILES as $file)
{
$file_info = $file['name'];
$extension = pathinfo($file['name'],PATHINFO_EXTENSION);
$destination_file_name = $imagesMoved . '-'. $sku . '.'. $extension;
$file_path = $images_directory . $destination_file_name;
if (move_uploaded_file($file['tmp_name'], $file_path))
{
$imagesMoved++;
//Move the file to S3
moveFileToS3($destination_file_name, $images_directory, $parent_folder . '/' . $images_folder . '/');
}
if (intval($imagesMoved) == intval($totalFileCount))
{
$image_moved = true;
//Begin to update the DB
}
}
未进行此调用(moveFileToS3)..用户选择的所有文件都使其成为实例“ A”。
但是,当进行此调用(moveFileToS3)时...并非用户选择的所有文件都将其转移到实例“ A”,并且只有实例“ A”中的一些文件移至S3位置。 $ image_moved = true之后的任何一条指令都不会执行。
为克服这种情况提供的任何帮助将不胜感激。我已附上包含方法“ moveFileToS3”的文件,供您快速参考。
<?php
require '../api/vendor/autoload.php';
use Aws\Common\Exception\MultipartUploadException;
use Aws\S3\MultipartUploader;
use Aws\S3\S3Client;
function moveFileToS3($fileName, $fileLocation, $targetLocation)
{
date_default_timezone_set('Asia/Kolkata');
$region = 'xxxxx';
$version = 'xxxxx';
$bucket = 'xx-xxxxxx-xxxxx';
$endpoint = 'xxxxxxxx.s3-accelerate.amazonaws.com';
$key = 'xxxxxx';
$secret = 'xxxxxxxxx';
$fileFullPathLocal = $fileLocation.$fileName;
$s3 = new S3Client([
'version' => $version,
'region' => $region,
'debug' => true,
'credentials' => [
'key' => $key,
'secret' => $secret,
]
]);
// Prepare the upload parameters.
$uploader = new MultipartUploader($s3, $fileFullPathLocal, [
'bucket' => $bucket,
'key' => $targetLocation.$fileName
]);
// Perform the upload.
try
{
$responseLogFile = fopen("../log/S3UploadLog_".date("Y-m-d").".log", "a+");
fwrite($responseLogFile, '['.date("Y-m-d H:i:s").']: Upload Started : '.$fileName. PHP_EOL. PHP_EOL);
fclose($responseLogFile);
$result = $uploader->upload();
$responseLogFile = fopen("../log/S3UploadLog_".date("Y-m-d").".log", "a+");
fwrite($responseLogFile, '['.date("Y-m-d H:i:s").']: Upload Finished : '.$fileName. PHP_EOL. PHP_EOL);
// fwrite($responseLogFile, '['.date("Y-m-d H:i:s").']: Upload Result : '.$result. PHP_EOL. PHP_EOL);
fwrite($responseLogFile, '['.date("Y-m-d H:i:s").']: Object Url : '.$result['ObjectURL']. PHP_EOL. PHP_EOL);
fclose($responseLogFile);
// echo "Upload complete: {$result['ObjectURL']}" . PHP_EOL;
unlink($fileFullPathLocal);
}
catch (MultipartUploadException $e)
{
$responseLogFile = fopen("../log/S3UploadLog_".date("Y-m-d").".log", "a+");
fwrite($responseLogFile, '['.date("Y-m-d H:i:s").']: Upload Failed : '.$fileName. PHP_EOL. PHP_EOL);
fclose($responseLogFile);
echo $e->getMessage() . PHP_EOL;
}
}
?>
答案 0 :(得分:0)
当您将文件从本地开发服务器移到存储桶时似乎会出现问题。但是当移至AWS环境时..没有任何问题