如何使用AWS sdk进行s3和ec2的文件操作?

时间:2015-12-02 17:24:58

标签: amazon-web-services amazon-s3 amazon-ec2 aws-sdk

我遇到了需要将文件写入S3和EC2的情况。

以下代码完全可以将文件写入S3,但不知道如何写入EC2。

    <?php

    if(file_exists('aws-autoloader.php')){

        require 'aws-autoloader.php';
    }else{

        die( 'File does not exist');
    }

    define('AWS_KEY','*****');
    define('AWS_SECRET','********');

    use Aws\S3\S3Client;
    use Aws\Credentials\Credentials;

    $credentials = new Credentials(AWS_KEY, AWS_SECRET);

    $client = new S3Client([
        'version'     => 'latest',
        'region'      => 'ap-southeast-1',
        'credentials' => $credentials
    ]);

    $client->registerStreamWrapper();

    file_put_contents('s3://mybucket/abc/a.txt', 'test');

    @chmod('/var/app/current', 0755);

//Not able to write the content to EC2 instance
    @file_put_contents('/var/app/current/b.txt', 'test');
    @file_put_contents('file://var/app/current/b.txt', 'test');
    ?>

1 个答案:

答案 0 :(得分:0)

如果应用程序具有写入权限,则对下面的代码没有任何问题。

    file_put_contents('/var/app/current/b.txt', 'test');
or
    file_put_contents('file:///var/app/current/b.txt', 'test');