无法上传工件没有HelloWorldFunction资源的CodeUri参数引用的对象

时间:2019-07-19 10:39:30

标签: amazon-web-services amazon-s3 amazon-cloudformation serverless-application-model

我正在关注this tutorial,以学习如何使用SAM

这是我的代码:

template.yml

AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs8.10

index.js

exports.handler = async function(event, context) {
    return 'Hello World!';
};

我跑步时

sam package \
  --template-file template.yml \
  --output-template-file package.yml \
  --s3-bucket brian-test-sam

我收到错误消息Unable to upload artifact None referenced by CodeUri parameter of HelloWorldFunction resource. An error occurred (AccessDenied) when calling the PutObject operation: Access Denied

为什么会这样?

我已经在我的AWS账户上创建了S3存储桶brian-test-sam。我检查了我的IAM用户是否具有AmazonS3FullAccess权限。

命令

sam --debug package \                                                                                           <aws:dev-bionime>
  --template-file template.yml \
  --output-template-file package.yml \
  --s3-bucket brian-test-sam

说该错误是由aws cloudformation package --output-template-file package.yml --s3-bucket brian-test-sam --template-file /path/to/my/files/helloworld/template.yml

产生的

我的cloudformation有什么问题?

我的aws cli版本是aws-cli/1.16.169 Python/3.7.3 Darwin/18.6.0 botocore/1.12.159。我的npm版本是6.10.1

5 个答案:

答案 0 :(得分:1)

即使遇到这个问题,我也采取了以下措施。

该问题是由于app.py和sam软件包命令中的存储桶不匹配,因此更正了存储桶名称并再次运行“ sam build”和“ sam软件包”命令,它对我有用!

请多加注意,如果您在运行“ sam软件包”时遇到与时间相关的问题,则系统时间应该无效,请更正它,然后再次运行“ sam软件包”。

答案 1 :(得分:1)

我发现此错误是因为我没有向运行构建的服务角色添加S3访问权限

在“角色”的“权限”选项卡上,选择“附加策略”按钮,然后选择“ AmazonS3FullAccess”,然后通过“附加策略”按钮附加它。

现在重新运行构建。

答案 2 :(得分:0)

您需要提供指向本地目录的CodeUri属性。

AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs8.10
      CodeUri: ./code

答案 3 :(得分:0)

在运行以下命令时,我遇到了同样的问题:

$response = $data['data'];
$shipping = $response["shipping"]["carriers"];

if ($response["hasVariations"]){
    $objProduct = new WC_Product_Variable();
} else{
    $objProduct = new WC_Product();
}

$objProduct->set_name($response["title"]);
$objProduct->set_status("publish");  // can be publish,draft or any wordpress post status
$objProduct->set_catalog_visibility('visible'); // add the product visibility status
$objProduct->set_description($response["htmlDescription"]);
$objProduct->set_sku($response["productId"]); //can be blank in case you don't have sku, but You can't add duplicate sku's
$objProduct->set_manage_stock(true); // true or false
$objProduct->set_stock_quantity($response["totalStock"]);
$objProduct->set_stock_status('instock'); // in stock or out of stock value
$objProduct->set_backorders('no');
$objProduct->set_reviews_allowed(true);
$objProduct->set_sold_individually(false);

if($response['hasSinglePrice']){
    $objProduct->set_regular_price($response['price']['web']['originalPrice']['value']); 
    if($response['price']['web']['hasDiscount']){
        $objProduct->set_sale_price($response['price']['web']['discountedPrice']['value']);
    }
}


$objProduct->set_category_ids(array(recentSearchCatID()));
// set attributes
$attributes= array();
$i= 0;
if($response['hasAttributes']){
    
    foreach($response["attributes"] as $attr){
        $att = array('id'=>0, 'name'=>$attr['name'], 'options'=>array($attr['value']['name']), 'visible'=> true, 'position'=>$i, 'variation'=>false );
        $attributes[]= create_attributes($att);
        $i++;
    }

}
if($response['hasProperties']){
    
    foreach($response["properties"] as $attr){
        
        $att = array('id'=>0, 'name'=>$attr['name'], 'visible'=> true, 'position'=>$i, 'variation'=>true );
        $options = array();
        foreach($attr['values'] as $opt){
            $options[]=$opt['name'];
        }
        $att['options']=$options;
        $attributes[]= create_attributes($att);
        $i++;
    }
    
}

$objProduct->set_attributes( $attributes );
$product_id = $objProduct->save();
if($response['hasVariations']) {
    foreach($response['variations'] as $vp) {
        $variation = new WC_Product_Variation();
        $variation->set_parent_id( $product_id );
        $props = array();
        foreach($vp['properties'] as $vpp){
            $props['pa_'.wc_sanitize_taxonomy_name($vpp['name'])] = wc_sanitize_taxonomy_name($vpp['value']['name']);
        }
        
        $variation->set_attributes($props);
        $variation->set_status('publish');
        $variation->set_sku($vp['sku']);
        
        if (array_key_exists("price",$vp)){
            $regularPrice = round($vp["price"]["web"]["originalPrice"]["value"]);
            $variation->set_regular_price($regularPrice);
            $discountPrice = NULL;
            if ($vp["price"]["web"]["hasDiscount"]){
                $discountPrice = round($vp["price"]["web"]["discountedPrice"]["value"]);
                $variation->set_sale_price($discountPrice);
            }
        }
        
        $variation->set_stock_status();
        $variation->set_stock_quantity(10);
        $variation->save();
        $product = wc_get_product($product_id);
        $product->save();
    }                    
    
}

exit();

以上命令缺少具有默认配置文件的AWS凭证。 [AWS配置中没有默认配置文件]如果未找到默认配置文件,则可能需要在以下命令中提供配置文件名称

sam package \
  --template-file template.yml \
  --output-template-file package.yml \
  --s3-bucket <your_bucket_name>

答案 4 :(得分:0)

Uploading to 5ede295b3d735d7cadf2a5368bcff051  124068 / 124068.0  (100.00%)
Successfully packaged artifacts and wrote output template to file package.yaml.
Execute the following command to deploy the packaged template..........

通过应用如下存储桶策略为我工作

{
    "Version": "2012-10-17",
    "Id": "Policy1624441771081",
    "Statement": [
        {
            "Sid": "Stmt1624441768741",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Resource": "<your_bucket_arn>/*"
        }
    ]
}