Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
除非手动单击“公开”,否则php无法访问s3对象_Php_Amazon S3_Permissions - Fatal编程技术网

除非手动单击“公开”,否则php无法访问s3对象

除非手动单击“公开”,否则php无法访问s3对象,php,amazon-s3,permissions,Php,Amazon S3,Permissions,我有一个amazon SES设置,可以在s3存储桶中转发/保存电子邮件 在s3中,我有一个bucket策略,其内容如下,支持自动将文件夹中的所有对象设置为公共(我知道这并不理想): 然后我有一个php脚本,用于列出s3 bucket中的对象,并抓取每个对象,以便将它们放入mysql 我的问题是,我可以列出没有任何问题的对象。然而,我的脚本试图解析每个对象的内容并将其保存到mysql表中,只有手动登录到s3并在每个对象上单击“公开”时,它才会起作用?那么我的s3桶策略似乎不起作用了?我知道我的另一

我有一个amazon SES设置,可以在s3存储桶中转发/保存电子邮件

在s3中,我有一个bucket策略,其内容如下,支持自动将文件夹中的所有对象设置为公共(我知道这并不理想):

然后我有一个php脚本,用于列出s3 bucket中的对象,并抓取每个对象,以便将它们放入mysql

我的问题是,我可以列出没有任何问题的对象。然而,我的脚本试图解析每个对象的内容并将其保存到mysql表中,只有手动登录到s3并在每个对象上单击“公开”时,它才会起作用?那么我的s3桶策略似乎不起作用了?我知道我的另一个选择是使用$s3->getObject()标记来获取对象,但我不确定如何将该标记与php文件\u get\u contents()方法一起使用,以便获取要解析的原始电子邮件对象文件

这是我的密码 当然,我需要提供登录/api密钥

<?php

//mysql connection 
$servername = "*****";
$username = "***";
$password ="****";
$databasename ="***";

$con = mysqli_connect("$servername","$username","$password","$databasename");

if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


//load the php mime parse library
require_once __DIR__.'/vendor/autoload.php';

$Parser = new PhpMimeMailParser\Parser();


//Include the AWS SDK using the Composer autoloader.
require 'awssdk/aws-autoloader.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
// AWS Info
$bucketName = 'pipedemail';
$IAM_KEY = '******';
$IAM_SECRET = '*****';
// Connect to AWS
try {
    // You may need to change the region. It will say in the URL when the bucket is open
    // and on creation. us-east-2 is Ohio, us-east-1 is North Virgina
    $s3 = S3Client::factory(
        array(
            'credentials' => array(
                'key' => $IAM_KEY,
                'secret' => $IAM_SECRET
            ),
            'version' => 'latest',
            'region'  => 'us-east-1'
        )
    );
} catch (Exception $e) {
    // We use a die, so if this fails. It stops here. Typically this is a REST call so this would
    // return a json object.
    die("Error: " . $e->getMessage());
}

// Use the high-level iterators (returns ALL of your objects).
$objects = $s3->getIterator('ListObjects', array('Bucket' => $bucketName));

foreach ($objects as $object) 
{
    $objectkey = $object['Key'];
    $path = "https://s3.amazonaws.com/pipedemail/$objectkey";

    //lets get the raw email file to parse it
    $Parser->setText(file_get_contents($path));

    // Once we've indicated where to find the mail, we can parse out the data
    //$to = $Parser->getHeader('to');             // "test" <test@example.com>, "test2" <test2@example.com>
    //$addressesTo = $Parser->getAddresses('to'); //Return an array : [[test, test@example.com, false],[test2, test2@example.com, false]]

    $from = $Parser->getHeader('from');             // "test" <test@example.com>

    $addressesFrom = $Parser->getAddresses('from'); //Return an array : test, test@example.com, false

    $subject = $Parser->getHeader('subject');

    //html of email body
    $html_emailbody = $Parser->getMessageBody('html');

    //insert the above pp variables data into a mysql table
    mysqli_query($con, "INSERT into emails(From_email,email_subject, email_body) VALUES('$from','$subject','$html_emailbody')");


    //now lets delete the object since we already took the email and saved it into mysql
    $s3->deleteObject(array('Bucket' => $bucketName, 'Key' => $objectkey)); 
}

是的,您应该使用ceejayoz建议的getObject方法。您需要确保与正在使用的IAM密钥/密码关联的IAM用户具有s3:GetObject权限,否则此调用将失败

然后,您的代码将类似于:

    $objectkey = $object['Key'];

    // Get the object
    $result = $s3->getObject(array(
        'Bucket' => $bucketName,
        'Key'    => $objectkey
    ));

    //lets get the raw email file to parse it
    $Parser->setText($result['Body']);

是的,您应该按照ceejayoz的建议使用getObject方法。您需要确保与正在使用的IAM密钥/密码关联的IAM用户具有s3:GetObject权限,否则此调用将失败

然后,您的代码将类似于:

    $objectkey = $object['Key'];

    // Get the object
    $result = $s3->getObject(array(
        'Bucket' => $bucketName,
        'Key'    => $objectkey
    ));

    //lets get the raw email file to parse it
    $Parser->setText($result['Body']);

您遇到了什么错误?请使用
$s3->getObject()
访问它们,或者在创建它们时将它们设置为公共。感谢您的回复ceejayoz我如何编辑我的代码以将$s3->getObject()标记与我的file_get_contents()函数一起使用?我尝试添加//让我们获取原始电子邮件文件来解析它$result=$s3->getObject(数组('Bucket'=>$bucketName,'Key'=>$objectkey))$解析器->setText(文件获取内容($result));但是这就导致$Parser->line上出现了500个错误。您会遇到什么错误?请使用
$s3->getObject()
访问它们,或者在创建它们时将它们设置为public。感谢您的回复ceejayoz,我如何编辑我的代码,以便将$s3->getObject()标记与我的file\u get\u contents()函数一起使用?我尝试添加//让我们获取原始电子邮件文件来解析它$result=$s3->getObject(数组('Bucket'=>$bucketName,'Key'=>$objectkey))$解析器->setText(文件获取内容($result));但是那只是在$Parser->line上造成了一个500错误。谢谢你,我的问题解决了。我真的很感谢你抽出时间来发帖。谢谢你这一周解决了我的问题。我真的很感谢你抽出时间来发帖。