Can I upload different files in different server using multipart uploads in Amazon s3 php at same time with same credentials?
I have created a application which will be in different servers when users click a button all the applications starts uploading files using same credentials.
try{
$s3 = S3Client::factory(array(
'key' => trim($as3_access_key),
'secret' => trim(str_replace(' ', '+', $as3_secure_key)),
// 'region' => 'us-west-2',
'region' => $as3_bucket_region,
'signature' => 'v4',
// 'LocationConstraint' => 'eu-central-1',
'ssl.certificate_authority' => false
));
$buckets = $s3->listBuckets();
}
catch (Exception $e){
return array(
'error' => 'Failed to upload to Amazon S3. Please check your details and set Managed Policies on your users to AmazonS3FullAccess.',
'error_code' => 'upload_failed_to_S3_check_your_details_and_set_managed_policies_amazonS3FullAccess_on_your_users',
'partial' => 1
);
}
if(filesize($backup_file) <5*1024*1024){
try{
$s3->putObject(array(
'Bucket' => $as3_bucket,
'SourceFile' => $backup_file,
'Key' => $as3_file,
'ACL' => 'public-read'
));
return true;
}
catch (Exception $e){
$err = $e->getMessage();
if($err){
return array(
'error' => 'Failed to upload to AmazonS3 ('.$err.').',
'error_code' => 'failed_upload_s3_with_error'
);
} else {
return array(
'error' => 'Failed to upload to Amazon S3.',
'error_code' => 'failed_upload_s3'
);
}
}
}
else {
$filename = $backup_file;
$result = $s3->createMultipartUpload(array(
'Bucket' => $as3_bucket,
'Key' => $as3_file,
'ACL' => 'public-read',
));
$uploadId = $result['UploadId'];
echo $uploadId;
// 3. Upload the file in parts.
try {
$file = fopen($filename, 'r');
$parts = array();
$partNumber = 1;
echo $partNumber;
while (!feof($file)) {
$result = $s3->uploadPart(array(
'Bucket' => $as3_bucket,
'Key' => $as3_file,
'UploadId' => $uploadId,
'PartNumber' => $partNumber,
'Body' => fread($file, 5 * 1024 * 1024),
));
$parts[] = array(
'PartNumber' => $partNumber++,
'ETag' => $result['ETag'],
);
echo "Uploading part {$partNumber} of {$filename}.\n";
}
fclose($file);
} catch (S3Exception $e) {
$result = $s3->abortMultipartUpload(array(
'Bucket' => $as3_bucket,
'Key' => $as3_file,
'UploadId' => $uploadId
));
echo "Upload of {$filename} failed.\n";
}
// 4. Complete multipart upload.
$result = $s3->completeMultipartUpload(array(
'Bucket' => $as3_bucket,
'Key' => $as3_file,
'UploadId' => $uploadId,
'Parts' => $parts,
));
$url = $result['Location'];
echo "Uploaded {$filename} to {$url}.\n";
return true;
}
it works fine but sometimes i get error like following
PHP Fatal error occurred: Uncaught Aws\S3\Exception\MalformedXMLException: AWS Error Code: MalformedXML, Status Code: 400, AWS Request ID: A5F83D5285F85FCC, AWS Error Type: client, AWS Error Message: The XML you provided was not well-formed or did not validate against our published schema, User-Agent: aws-sdk-php2/2.7.27 Guzzle/3.9.3 curl/7.39.0 PHP/5.6.3 thrown in E:\xampp\htdocs\1\wp-content\plugins\iwp-client\lib\amazon\vendor\aws\aws-sdk-php\src\Aws\Common\Exception\NamespaceExceptionFactory.php on line 91.
is this function using same credentials,same bucket at simultaneously in different location.
Aucun commentaire:
Enregistrer un commentaire