AWS CodeBuild无法同步到S3存储桶ListObject拒绝权限

时间:2018-09-12 03:06:48

标签: amazon-web-services amazon-s3 amazon-cloudfront aws-codebuild

在CodeBuild中,我有2个项目。一个用于暂存站点,另一个用于生产站点。当我编译站点并通过暂存项目运行它时,它可以正常工作。它已成功同步到我的暂存站点的s3存储桶。但是,当尝试编译它并通过生产项目运行它时,在运行sync命令时,它将返回错误:

  

严重错误:调用ListObjects操作时发生错误(AccessDenied):访问被拒绝

     

[容器] 2018/09/11 08:40:33命令未成功退出aws s3 sync public / s3:// $ S3_BUCKET退出状态1

我做了一些挖掘工作,我认为问题出在我的存储桶策略上。我将CloudFront用作S3存储桶顶部的CDN。在我绝对确定必须这样做之前,我不想立即修改生产存储桶的存储桶策略。我担心这可能会对实时网站产生影响。 这是我的生产时段的时段政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::[bucket_name]/*"
        },
        {
            "Sid": "2",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity [access_code]"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::[bucket_name]/*"
        }
    ]
}

1 个答案:

答案 0 :(得分:2)

根据错误描述,缺少列表权限。

在您的存储桶策略中添加以下权限:

    private ServiceLocator() throws Exception{
        createContext();
    }

    public void createContext() throws Exception{
        Hashtable env = new Hashtable();
        cachedObject=new HashMap(10,0.8f);
        env.put(Context.INITIAL_CONTEXT_FACTORY, ConfigurationManager.getInstance().getPropertyValue(WebConstants.INITIAL_CONTEXT_FACTORY));
        System.out.println(ConfigurationManager.getInstance().getPropertyValue(WebConstants.INITIAL_CONTEXT_FACTORY));

        env.put(Context.PROVIDER_URL, ConfigurationManager.getInstance().getPropertyValue(WebConstants.PROVIDER_URL));
        env.put(Context.URL_PKG_PREFIXES, ConfigurationManager.getInstance().getPropertyValue(WebConstants.URL_PKG_PREFIXES));
        env.put("jboss.naming.client.ejb.context", true);
        context = new WildFlyInitialContext(env);
        System.out.println("context: "+context);
        log.debug("ServiceLocator createContext method ends ");

    }    

    public EJBHome getHome(String ejbName) throws Exception{
        EJBHome ejbHome = null;
        log.debug("ServiceLocator getHome() starts ");
        try{
            System.out.println("Context: "+context);
            if(context != null){
                Object home = context.lookup("ejb:/"+ejbName);
                System.out.println(home);
                ejbHome = (EJBHome) home;
                System.out.println(ejbHome);
            }
        }catch(Exception e){
            log.error("Exception whichle retrieving the home object for the EJB : " + ejbName);
            log.error(e.getMessage());
            throw new Exception(e);
        }
        log.debug("ServiceLocator getHome() ends ");
        return ejbHome;
    }

这应该可以解决您的问题。另外,请检查在Videocapture cap = new VideoCapture(); cap.open("nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720, format=(string)I420, framerate=(fraction)120/1 ! nvvidconv flip-method=2 ! video/x-raw, format=(string)I420 ! videoconvert ! video/x-raw, format=(string)BGR ! appsink"); cap.read(... 上创建的 "Action": [ "s3:Get*", "s3:List*" ] 服务角色,以访问IAM存储桶。在这种设置中,codebuild存储桶策略和S3角色都控制对S3存储桶的访问。

您的服务角色应具有IAM的列表权限。

S3