Sonata + Amazon S3 - 无法编写关键内容

Posted

技术标签:

【中文标题】Sonata + Amazon S3 - 无法编写关键内容【英文标题】:Sonata + Amazon S3 - Could not write the key content 【发布时间】:2015-05-27 22:54:18 【问题描述】:

我确实安装了 Sonata Media 的 dev-master 版本(+所有要求),它工作正常。现在我正在尝试使用 amazon s3 文件服务来上传我的图像和文件。 (我在本地主机上使用 wamp)

这是我的配置:

#...
sonata_media:
    default_context: default
    db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr it is mandatory to choose one here
    default_context: default # you need to set a context
    contexts:
        default:  # the default context is mandatory
            providers:
                - sonata.media.provider.dailymotion
                - sonata.media.provider.youtube
                - sonata.media.provider.image
                - sonata.media.provider.file

            formats:
                small:  width: 100 , quality: 70
                big:    width: 500 , quality: 70

    cdn:
        server:
            path: http://mybucketname.s3-website-us-east-1.amazonaws.com
    providers:
        image:
            filesystem: sonata.media.filesystem.s3
    filesystem:
        s3:
            bucket: #MyBundlename
            accessKey: #MyAccessKey
            secretKey: #Mysecret key
            region: s3-website-us-east-1.amazonaws.com
            storage: standard
            acl: public #I tried private too

所以对于我的密钥,我尝试使用所有者密钥和我使用列表+上传/删除权限创建的用户密钥

这也是我的存储桶策略


    "Version": "2012-10-17",
    "Statement": [
        
            "Sid": "AddPerm",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::mybcucketname/*"
        
    ]

当我尝试使用奏鸣曲媒体包上传图像时,我遇到了这个错误:

Could not write the "default/0001/01/634f09dfda5705a8310c084a92f686ec33449960.png" key content.

这也是我的 composer.json 文件:

"php": ">=5.3.3",
    "symfony/symfony": "2.6.*",
    "doctrine/orm": "~2.2,>=2.2.3,<2.5",
    "doctrine/dbal": "<2.5",
    "doctrine/doctrine-bundle": "~1.2",
    "twig/extensions": "~1.0",
    "symfony/assetic-bundle": "~2.3",
    "symfony/swiftmailer-bundle": "~2.3",
    "symfony/monolog-bundle": "~2.4",
    "sensio/distribution-bundle": "~3.0,>=3.0.12",
    "sensio/framework-extra-bundle": "~3.0,>=3.0.2",
    "incenteev/composer-parameter-handler": "~2.0",
    "sonata-project/core-bundle": "2.3.*@dev",
    "sonata-project/intl-bundle": "2.2.*@dev",
    "sonata-project/admin-bundle": "2.4.*@dev",
    "sonata-project/doctrine-orm-admin-bundle": "2.4.*@dev",
    "sonata-project/datagrid-bundle": "2.2.*@dev",
    "sonata-project/classification-bundle": "dev-master",
    "jms/serializer-bundle": "~0.13",
    "sonata-project/easy-extends-bundle": "2.1.*@dev",
    "sonata-project/media-bundle": "2.4.*@dev",
    "aws/aws-sdk-php": "2.*"

这是亚马逊 s3 日志

" 403 AccessDenied 231 149653 16 - "

(日志其余部分正确,桶名和用户) 谢谢您的帮助。 哟韩。

【问题讨论】:

好的,我不知道我所做的是否正确且安全,但是: 【参考方案1】:

我不知道我所做的是否是最好的解决方案并且是安全的,但是: - 我编辑了存储桶策略:

旧的:


"Version": "2012-10-17",
"Statement": [
    
        "Sid": "AddPerm",
        "Effect": "Allow",
        "Principal": "*",
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::mybucket/*"
    
]

新的:


    "Version": "2012-10-17",
    "Statement": [
        
            "Sid": "AddPerm",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::mybucket/*"
        ,
        
            "Sid": "AddCannedAcl",
            "Effect": "Allow",
            "Principal": 
                "AWS": [
                    "arn:aws:iam::XXXXXXXXXXXX:user/myuser"
                ]
            ,
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl"
            ],
            "Resource": [
                "arn:aws:s3:::mybucket/*"
            ],
            "Condition": 
                "StringEquals": 
                    "s3:x-amz-acl": [
                        "public-read"
                    ]
                
            
        
    ]

其中 arn:aws:iam::XXXXXXXXXXXX:user/myuser 是您创建的具有列表上传/删除权限的用户。

【讨论】:

这个答案有效吗?因为我解决了同样的问题,但在我的情况下,奏鸣曲媒体包显示图像已上传,但奏鸣曲媒体给出的图像 url 显示错误:-“指定的密钥不存在。”【参考方案2】:

我的问题是我在 s3 配置中有默认的 acl 值。

我改成:

filesystem:
        s3:
            directory:  'upload/media'
            acl: private   #<------ this is what i change
            bucket:      '%env(resolve:S3_MEDIA_BUCKET_NAME)%'
            accessKey:   '%env(resolve:S3_MEDIA_ACCESS_KEY)%'
            secretKey:   '%env(resolve:S3_MEDIA_SECRET_KEY)%'
            region:      '%env(resolve:S3_MEDIA_REGION)%'
            version:     '2006-03-01' 
            sdk_version: 3

希望对你有帮助。

【讨论】:

以上是关于Sonata + Amazon S3 - 无法编写关键内容的主要内容,如果未能解决你的问题,请参考以下文章

在 Amazon s3 中将 Spark RDD 编写为 Gzipped 文件

Amazon S3 直接上传无法识别文件的内容类型

Amazon S3,无法设置 CORS 策略

Amazon S3 无法通过 Cloudfront 上传文件

无法将应用程序上传到 Amazon S3

Amazon Aurora 1.8 从 S3 加载数据 - 无法实例化 S3 客户端