将文件从本地复制到s3并删除

时间:2017-11-21 07:32:27

标签: amazon-web-services amazon-s3

我正在尝试将文件同步到s3,然后需要在源代码中删除。有人可以帮助我使用脚本或命令吗?

2 个答案:

答案 0 :(得分:1)

aws s3 sync /Path/to/local/dir s3://YOUR_BUCKET_NAME ;

rm -rf /Path/to/local/dir/*

确保您具有在该存储桶上传的正确权限(策略文档)。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket",
                "s3:PutObject",
                "s3:PutObjectAcl"
            ],
            "Resource": [
                "arn:aws:s3:::YOUR_BUCKET_NAME/*",
                "arn:aws:s3:::YOUR_BUCKET_NAME"
            ]
        }
    ]
}

答案 1 :(得分:0)

您可以使用以下代码将备份文件上传到S3存储桶

    $awsAccessKey = AWS_ACCESS_KEY;
    $awsSecretKey = AWS_SECRET_ACCESS_KEY;
    $bucket = AWS_BUCKET_NAME;
    $zipfile = 'path for your zip file';

    $s3 = new \S3($awsAccessKey, $awsSecretKey);

    $s3->putBucket($bucket, \S3::ACL_PUBLIC_READ);

    $s3->putObjectFile($path."/".$zipfile, $bucket , $zipfile, \S3::ACL_PUBLIC_READ);

并从S3桶中删除文件代码可以是用户: -

    $awsAccessKey = AWS_ACCESS_KEY;
    $awsSecretKey = AWS_SECRET_ACCESS_KEY;
    $bucket = AWS_BUCKET_NAME;
    $zipfile = 'file name';

    $s3 = new \S3($awsAccessKey, $awsSecretKey);

    $url = "http://".$bucket.".s3.amazonaws.com/".$zipfile;
    if($this->is_url_exist($url))
        $s3->deleteObject($bucket, $zipfile);