有没有办法一次上传多个文件,而不必为每个文件重新连接?
我使用S3作为我的php应用程序的存储空间,它需要存储大多数(大约10k)图像文件的大数字(一次100个)。目前我正在循环浏览它们并使用以下代码单独上传:
$s3->putObjectFile($uploadFile, $bucketName, ($uploadFile), S3::ACL_PUBLIC_READ)
这需要很长时间。 1.5兆的文件大概一分钟。如其他答案中所建议的那样关闭SSL会减少到大约40秒,但这仍然很慢。
这是我当前的代码,使用PHP的Amazon S3 REST实现
$s3 = new S3($awsAccessKey, $awsSecretKey, false);
function send_to_s3($s3, $bucketName, $uploadFile)
{
$start = microtime(true);
// Check if our upload file exists
if (!file_exists($uploadFile) || !is_file($uploadFile))
exit("\nERROR: No such file: $uploadFile\n\n");
// Check for CURL
if (!extension_loaded('curl') && !@dl(PHP_SHLIB_SUFFIX == 'so' ? 'curl.so' : 'php_curl.dll'))
exit("\nERROR: CURL extension not loaded\n\n");
if ($s3->putObjectFile($uploadFile, $bucketName, ($uploadFile), S3::ACL_PUBLIC_READ))
{
$end = microtime(true);
$took = $end - $start;
echo "S3::putObjectFile(): File copied to {$bucketName}/".($uploadFile).PHP_EOL . ' - ' . filesize($uploadFile) . ' in ' . $took . ' seconds<br />';
return $took;
}
else
{
print 'error';
}
}
感谢任何帮助。
答案 0 :(得分:1)
use Aws\S3\S3Client;
use Aws\CommandPool;
use Guzzle\Service\Exception\CommandTransferException;
$commands = array();
foreach ( $objects as $key => $file ) {
$fileContent = $file['body'];
$objParams = array (
'ACL' => 'bucket-owner-full-control',
'Bucket' => 'bucket_name',
'Key' => 's3_path',
'Body' => $fileContent
);
$commands[] = $clientS3->getCommand('PutObject', $objParams);
}
try {
$results = CommandPool::batch($clientS3, $commands);
} catch (CommandTransferException $e) {
$succeeded = $e->getSuccessfulCommands();
echo "Failed Commands:\n";
foreach ($e->getFailedCommands() as $failedCommand) {
echo $e->getExceptionForFailedCommand($failedCommand)->getMessage() . "\n";
}
}
答案 1 :(得分:0)
我建议通过分段上传来上传图片。
以下代码是使用Aws-sdk版本3的基本示例。
use Aws\S3\MultipartUploader;
use Aws\Exception\MultipartUploadException;
$uploader = new MultipartUploader($s3Client, '/path/to/large/file.zip', [
'bucket' => 'your-bucket',
'key' => 'my-file.zip',
]);
try {
$uploader->upload();
echo "Upload complete.\n";
} catch (MultipartUploadException $e) {
echo $e->getMessage() . "\n";
}