简体   繁体   English

使用预签名URL将mp3 / zip文件上传到AWS S3

[英]Uploading mp3/zip file to AWS S3 with Pre-signed URL

I am trying to upload .mp3 file to Amazon S3 Server Using this code, i am not getting any resposne from server. 我试图将.mp3文件上传到Amazon S3服务器,使用此代码,我没有从服务器得到任何回购。 On other hand .txt OR .pdf OR .odf file is working fine. 另一方面,.txt或.pdf或.odf文件工作正常。 What i do for .mp3 and .zip file Structure. 我为.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();
                    }
  <?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);     
        }
     }
    } 
    ?>

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM