我正在尝试将.mp3文件上传到Amazon S3服务器使用此代码,我没有从服务器获得任何转发。另一方面.txt或.pdf或.odf文件工作正常。我为.mp3和.zip文件结构做了什么。
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Common\Aws;
use Aws\Common\Enum\Size;
use Aws\Common\Exception\MultipartUploadException;
use Aws\S3\Model\MultipartUpload\UploadBuilder;
use Aws\S3\Model\ClearBucket;
$buketname = '***********';
$filename = 'first.mp3';
$fileloc = '/var/www/html/aws/aws-sdk-php/first.mp3';
// 1. Instantiate the client.
$s3 = S3Client::factory(array(
'credentials' => array(
'key' => '**************',
'secret' => '**************',
)
));
try {
$result = $s3->putObject(array(
'Content-Type' => 'audio/mpeg',
'Bucket' => $buketname,
'Key' => $filename,
'SourceFile' => $fileloc,
'Metadata' => array(
'Agent' => 'xyz'
)
));
// We can poll the object until it is accessible
$s3->waitUntilObjectExists(array(
'Bucket' => $buketname,
'Key' => $filename
));
echo "File Uploaded : ".$filename;
} catch (\Aws\S3\Exception\S3Exception $e) {
echo $e->getMessage();
}
答案 0 :(得分:0)
<?php
ob_start();
/* Amazon A3 */
// Auto Load Things i need
require 'vendor/autoload.php';
// Define Things i will use
use Aws\Common\Aws;
use Aws\Common\Enum\Size;
use Aws\Common\Exception\MultipartUploadException;
use Aws\S3\Model\MultipartUpload\UploadBuilder;
use Aws\S3\Model\ClearBucket;
// Static Variables
$aws_access_key = AWSACCESSKEY; // AWS Access key
$aws_access_security = AWSACCESSSECURITY; // AWS Security Key
$aws_default_buket = AWSDEFAULTBUKET; // Your Default Bucket
$aws_default_region = AWSDEFAULTREGION; // Your Default Region
$aws_default_scema = AWSDEFAULTSCEMA; // Default Protocol Schema
$aws_default_uploadfrom = AWSDEFAULTUPLOADFROM; // File Upload from Directory
// Instantiate the AWS client with your AWS credentials
$aws = Aws::factory(array(
'key' => $aws_access_key,
'secret' => $aws_access_security,
'region' => $aws_default_region,
'scheme' => $aws_default_scema,
));
// Define S3client Object
$s3Client = $aws->get('s3');
$useridfolder = $_REQUEST["useridfolder"];
if(!empty($_FILES['inputFilemp3']['name'])) {
$timestamp = time();
$uploaddir = "mp3uploads/";
$filename = $_FILES['inputFilemp3']['name'];
//$filename = strtolower($filename);
$filename = strip_tags($filename);
//$str = preg_replace('/[\r\n\t ]+/', ' ', $str);
//$str = preg_replace('/[\"\*\/\:\<\>\?\'\|]+/', ' ', $str);
//$str = strtolower($str);
//$str = html_entity_decode( $str, ENT_QUOTES, "utf-8" );
//$str = htmlentities($str, ENT_QUOTES, "utf-8");
//$str = preg_replace("/(&)([a-z])([a-z]+;)/i", '$2', $str);
//$str = str_replace(' ', '-', $str);
//$str = str_replace('--','-',$str);
//$str = rawurlencode($str);
//$str = str_replace('%', '-', $str);
//$str = str_replace('#', '', $str);
//$str = ltrim($str, '-');
//$filename = preg_replace('/-{2,}/','-',$str);
///new line
$final_location = "mp3uploads/".$useridfolder."/".$timestamp."/".$filename;
/* crete and prepare directories */
/* file type check */
$type = $_FILES["inputFilemp3"]["type"];
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed','audio/mpeg','audio/x-mpeg','audio/mp3','audio/x-mp3','audio/mpeg3','audio/x-mpeg3','audio/mpg','audio/x-mpg','audio/x-mpegaudio');
$okay = false;
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
}
}
if(($okay == false) || ($_FILES["inputFilemp3"]["size"] > 157286400)){
echo "INVALID FILE";
} else{
/* file upload action */
if ($_FILES["inputFilemp3"]["error"] > 0)
{
echo " INVALID FILE";
} else {
$buketname = $aws_default_buket; // Get Bucket name
$filename = $final_location;
$fileloc = $_FILES['inputFilemp3']['tmp_name'];
$fileacl = 'private'; // private | public-read | public-read-write | authenticated-read | bucket-owner-read | bucket-owner-full-control
if(!$buketname || !$filename || !$fileloc || !$fileacl){
echo "Dude! your commands don't seems like they are ok!";
die();
}
try {
$uploader = UploadBuilder::newInstance()
->setClient($s3Client)
->setSource($fileloc)
->setBucket($buketname)
->setKey($filename)
->setConcurrency(3)
->setOption('ACL', $fileacl)
->setOption('Metadata', array('Agent' => 'aisS3Client'))
->setOption('CacheControl', 'max-age=7200')
->build();
// Perform the upload. Abort the upload if something goes wrong
try {
$uploader->upload();
//echo "File Uploaded : ".$filename;
} catch (MultipartUploadException $e) {
$uploader->abort();
//echo "File Did not Uploaded : ".$filename;
}
} catch (\Aws\S3\Exception\S3Exception $e) {
echo $e->getMessage();
}
unlink($_FILES['inputFilemp3']['tmp_name']);
$download_url= AWSDOWNLOADURL.$filename;
echo $download_url = trim($download_url);
}
}
}
?>