如何将公共文件读写到Amazon S3?

时间:2016-02-16 13:14:18

标签: laravel amazon-s3 laravel-5 amazon

我正在尝试编写并将文件下载到Amazon S3。该文件已加载但我无法取回:

public function putFile() 
{
    $url = 'https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/p720x720/12631558_953568381347717_3978737099026165391_n.jpg?oh=9e14f5ea6f8e3a3fc8ffa07095d4c766&oe=57321C9D&__gda__=1463610491_c1f275837b7f5b4d2041597a3d7bb9c8';        
    $file = file_get_contents($url);

    $s3 = Storage::disk('s3');
    $s3->put('file.jpg', $file);        
}

public function getFile() 
{
     $contents = $s3->get('file.jpg');        
     dd($contents);
}

getFile()返回:

    FileNotFoundException in FilesystemAdapter.php line 58:
    file.jpg

我的Bucket权限:

    Grantee: mgalex (List, Update/Delete, View Permissions, Edit Permissions)
    Grantee: Any Authentificated AWS User (List, Update/Delete, View Permissions)
    Grantee: Everyone (List, Update/Delete, View Permissions)

但是文件是使用权限创建的:

    Grantee: mgalex (Open/Download, View Permissions, Edit Permissions)

我编辑了CORS。我试着解决它。问题仍然存在

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">          
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod> 
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>DELETE</AllowedMethod>
        <AllowedHeader>*</AllowedHeader>       
    </CORSRule>
</CORSConfiguration>

重要提示:如果我手动设置了“任何身份验证的AWS用户”或“所有人”的权限,则代码可以正常运行。

编辑2:我尝试以其他方式上传文件并将ACL添加到请求中(canned ACL

public function putFile2() 
{
    $bucket = config('filesystems.disks.s3.bucket');
    $fileName = 'file2.jpg';

    $url = 'https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/p720x720/12631558_953568381347717_3978737099026165391_n.jpg?oh=9e14f5ea6f8e3a3fc8ffa07095d4c766&oe=57321C9D&__gda__=1463610491_c1f275837b7f5b4d2041597a3d7bb9c8';        
    $fileBody = file_get_contents($url);

    $disk = Storage::disk('s3');
    $client = $disk->getDriver()->getAdapter()->getClient();

    $client->putObject([
        'Bucket' => $bucket,
        'Key'    => $fileName,
        'Body'   => $fileBody,
        'ACL'    => 'public-read-write'
    ]);
}

此返回错误:AccessDenied。

我有两个问题:

  1. 如果我使用正确的密钥连接到S3,为什么我无法从代码中获取文件?
  2. 如何将文件公开提供给服务器(受赠人:每个人)?有可能吗?
  3. 请帮忙。我很长时间都在寻找答案,并没有解决问题

0 个答案:

没有答案