使用PHPSDK在AmazonS3上上载文件

使用PHPSDK在AmazonS3上上载文件,php,amazon-web-services,upload,amazon-s3,Php,Amazon Web Services,Upload,Amazon S3,我正试图通过他们的PHP SDK在AmazonS3上上传一张图片。所以我做了一个小脚本来做。但是,我的脚本不工作,我的异常不会向我返回任何错误消息 我是AWS的新手,谢谢你的帮助 代码如下: Config.php <?php return array( 'includes' => array('_aws'), 'services' => array( 'default_settings' => array( 'params' => array(

我正试图通过他们的PHP SDK在AmazonS3上上传一张图片。所以我做了一个小脚本来做。但是,我的脚本不工作,我的异常不会向我返回任何错误消息

我是AWS的新手,谢谢你的帮助

代码如下:

Config.php

<?php 

return array(
'includes' => array('_aws'),
'services' => array(
  'default_settings' => array(
      'params' => array(
          'key'    => 'PUBLICKEY',
          'secret' => 'PRIVATEKEY',
          'region' => 'eu-west-1'
      )
    )
  )
);

?>

Index.php

 <?php


//Installing AWS SDK via phar
require 'aws.phar';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

$bucket = 'infact';
$keyname = 'myImage';

// $filepath should be absolute path to a file on disk                      
$filepath = 'image.jpg';

// Instantiate the client.
$s3 = S3Client::factory('config.php');

// Upload a file.
try {

$result = $s3->putObject(array(
    'Bucket'       => $bucket,
    'Key'          => $keyname,
    'SourceFile'   => $filePath,
    'ContentType'  => 'text/plain',
    'ACL'          => 'public-read',
    'StorageClass' => 'REDUCED_REDUNDANCY'
));

 // Print the URL to the object.
    echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

?>

编辑:我现在正在使用此代码,但它仍然不起作用。我甚至没有错误或异常消息

    <?php

require 'aws.phar';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

$bucket = 'infactr';
$keyname = 'sample';
// $filepath should be absolute path to a file on disk                      
$filepath = 'image.jpg';

// Instantiate the client.
$s3 = S3Client::factory(array(
    'key'    => 'key',
    'secret' => 'privatekey',
    'region' => 'eu-west-1'

    ));

try {
    // Upload data.
    $result = $s3->putObject(array(
        'Bucket' => $bucket,
        'Key'    => $keyname,
        'SourceFile'   => $filePath,
        'ACL'    => 'public-read',
        'ContentType' => 'image/jpeg'
    ));

    // Print the URL to the object.
    echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

?>

试试这样的方法(从中):


只要你有合适的证书,我就可以用。请记住,密钥名是S3中文件的名称,因此如果希望密钥与文件名相同,则必须执行如下操作:
$keyname='image.jpg'。此外,jpg通常不是
纯/文本
文件类型,您可以使用
内容类型
字段,或者只需指定:
图像/jpeg

$s3 = S3Client::factory('config.php');
应该是

$s3 = S3Client::factory(include 'config.php');

另一种解释方法是展示curl,以及如何在php中构建它——这是一种实用的方法。 请不要因为丑陋的代码而责怪我,我只是认为这个例子很容易从PHP或其他语言上传到Azure

$azure1 ='https://viperprodstorage1.blob.core.windows.net/bucketnameAtAzure/';
$azure3 ='?sr=c&si=bucketnameAtAzure-policy&sig=GJ_verySecretHashFromAzure_aw%3D';
$shellCmd='ls -la '.$outFileName;
$lsOutput=shell_exec($shellCmd);
#print_r($lsOutput);
$exploded=explode(' ', $lsOutput);
#print_r($exploded);
$fileLength=$exploded[7];
$curlAzure1="curl -v -X PUT -T '" . $outFileName . "' -H 'Content-Length: " . $fileLength . "' ";
$buildedCurlForUploading=$curlAzure1."'".$azure1.$outFileName.$azure3."'";
var_dump($buildedCurlForUploading);
shell_exec($buildedCurlForUploading);
这是实际的旋度

shell_exec("curl -v -X PUT -T 'fileName' -H 'Content-Length: fileSize' 'https://viperprodstorage1.blob.core.windows.net/bucketnameAtAzure/fileName?sr=c&si=bucketNameAtAzure-policy&sig=GJ_verySecretHashFromAzure_aw%3D'")

下面是AmazonS3 bucket中上传图像/文件的代码

function upload_agreement_data($target_path, $source_path, $file_name, $content_type)
{
    $fileup_flag = false;

    /*------------- call global settings helper function starts ----------------*/
    $bucketName = "pilateslogic";
    //$global_setting_option = '__cloud_front_bucket__';
    //$bucketName = get_global_settings($global_setting_option);
    /*------------- call global settings helper function ends ----------------*/

    if(!$bucketName)
    {
        die("ERROR: Template bucket name not found!");
    }

    // Amazon profile_template template js upload URL
    $target_profile_template_js_url = "/".$bucketName."/".$target_path;

    // Chatching profile_template template js upload URL
    //$source_profile_template_js_url = dirname(dirname(dirname(__FILE__))).$source_path."/".$file_name;

    // file name
    $template_js_file = $file_name;

    $this->s3->setEndpoint("s3-ap-southeast-2.amazonaws.com");
    if($this->s3->putObjectFile($source_path, $target_profile_template_js_url, $template_js_file, S3::ACL_PUBLIC_READ, array(), array("Content-Type" => $content_type)))
    {
        $fileup_flag = true;
    }
    return $fileup_flag;
}

对于那些寻找最新工作版本的人,这就是我正在使用的

// Instantiate the client.
$s3 = S3Client::factory(array(
    'credentials' => [
        'key'    => $s3Key,
        'secret' => $s3Secret,
        ],
    'region' => 'us-west-2',
    'version' => "2006-03-01"
));

try {
    // Upload data.
    $result = $s3->putObject(array(
        'Bucket' => $s3Bucket,
        'Key'    => $fileId,
        'SourceFile'   => $filepath."/".$fileName
    ));
    return $result['ObjectURL'];
} catch (S3Exception $e) {
    return false;
}

您是从Web还是从命令行运行此操作?请从命令行尝试查看错误。我从来没有在s3上使用过任何控制台。您正在使用连接吗?是
s3cmd
。您不需要
s3cmd
来尝试您的脚本。您只需从Linux命令行$source\u path=$\u SERVER['DOCUMENT\u ROOT']运行:
php.php
。“/uploads/”$advertiseImage;$file_name=rand();$content\u type=“图像/jpeg”;$target_path=“FreeImage”;$this->upload\u agreement\u data($target\u path、$source\u path、$file\u name、$content\u type);函数getBucketURL($imageType,$uri){$bucket=“pilateslogic”;$newUri=$imageType。”/“$uri;$fileURL=$this->s3->getAuthenticateUrl($bucket,$newUri,1000,$hostBucket=false,$https=false);返回$fileURL;}
// Instantiate the client.
$s3 = S3Client::factory(array(
    'credentials' => [
        'key'    => $s3Key,
        'secret' => $s3Secret,
        ],
    'region' => 'us-west-2',
    'version' => "2006-03-01"
));

try {
    // Upload data.
    $result = $s3->putObject(array(
        'Bucket' => $s3Bucket,
        'Key'    => $fileId,
        'SourceFile'   => $filepath."/".$fileName
    ));
    return $result['ObjectURL'];
} catch (S3Exception $e) {
    return false;
}