尝试通过NodeJS Lambda将XML网站地图放入AWS S3存储桶

时间:2018-10-25 22:00:31

标签: javascript node.js amazon-web-services amazon-s3 aws-lambda

我已经配置了一个AWS S3存储桶以存储站点地图XML文件。这些将通过AWS lambda中的某些功能定期更新。我有一个lambda发送到我的S3存储桶中的sitemap.xml对象(这不是实际文件,它是在站点爬网后构建的,然后即时将其格式化为XML的站点地图),但是什么也没发生。我在lambda日志或S3日志中均未收到任何错误。我尝试向S3发出请求的lambda附加了AmazonS3FullAccess策略。

我可以在S3日志中看到它被触发,并且日志显示所有200s,但是在S3存储桶中看不到我的数据。为了进行故障排除,我什至放松了S3存储桶策略以接受所有请求。仍然没有运气。

一个奇怪的是,一旦我的S3存储桶被触发,它就会不断不断地生成日志;我的请求已关闭很久以后。当我打开日志文件时,其中一些显示一堆成功的put请求,另一些显示404s和我什至没有发出的请求中的所有错误!例如,我昨晚试图写我的S3存储桶。今天早上,我生成了300个日志文件,但S3存储桶中什么也没有。日志文件具有200s,400s,404s,具体取决于请求的类型(REST.GET.BUCKET,BATCH.DELETE.OBJECT,REST.GET.NOTIFICATION,REST.GET.WEBSITE,REST.PUT.BUCKETPOLICY等)。我只发出一个PUT请求,所以我不知道为什么日志会显示所有这些请求。

这是我的代码:

[bunch of imports and AWS config stuff]

exports.handler = async(event) => {
  let urlArray;
  let metadataArray;
  let sitemapObject;
  const parsedData = JSON.parse(event.Records[0].Sns.Message);

  try {
    // separating two different arrays of objects from an outer array    
    metadataArray = parsedData[0];
    urlArray = parsedData[1];

    const newlyMergedArray = await mergeSpecificDataFromBothArrays(urlArr, vidArr);

    sitemapObject = await generateSitemap(newlyMergedArray);
  } catch (error) {
    throw error;
  }

    const dispatchToS3 = () => {

    const params = {
      Body: sitemapObject,
      Bucket: 'sitemap-service-s3',
      Key: 'my_website.com/sitemap.xml'
    };

    s3.putObject(params, (error, data) => {
      if (error) {
        console.log(this.httpResponse.body.toString());
        console.log(this.httpResponse.headers);
        console.log(error.stack);
        throw error;
      } else {
        console.log(data);
      }
    });
  };

try {
    await dispatchToS3();
  } catch (error) {
    throw error;
  }
}

sitemapObject是一个在我将其发送到S3之前就已转换为XML的对象。 S3可以接受XML吗?如果是这样,我应该在执行JSON.stringify之前对其应用putObject吗?虽然,我尝试将sitemapObject作为字符串化和非字符串化数据发送。 putObject在任何情况下均不执行任何操作。我应该以实际的JSON对象而不是XML格式发送此数据吗?

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<url>
<loc>https://www.mywebsite.com/path/to/video_url</loc>
<video:video>
<video:title>Video Title/video:title>
<video:description/>
<video:thumbnail_loc>http://mywebsite.com/path/to/thumbnail</video:thumbnail_loc>
<video:player_loc>https://third_party_video_hosting_site.com/path/to/player</video:player_loc>
<video:duration>2:06</video:duration>
</video:video>
</url>
<url>
<loc>https://www.mywebsite.com/path/to/video_url</loc>
<video:video>
<video:title>Video Title/video:title>
<video:description/>
<video:thumbnail_loc>http://mywebsite.com/path/to/thumbnail</video:thumbnail_loc>
<video:player_loc>https://third_party_video_hosting_site.com/path/to/player</video:player_loc>
<video:duration>2:06</video:duration>
</video:video>
</url>
</urlset>

这是我的S3存储桶策略:

{
    "Version": "2012-10-17",
    "Id": "Policy12345678910",
    "Statement": [
        {
            "Sid": "Stmt12345678910",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*" // this wildcard is for troubleshooting only. I would make this more specific/secure once I'm no longer troubleshooting.
            },
            "Action": [
                "s3:DeleteObject",
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Resource": "arn:aws:s3:::sitemap-service-s3/my_website.com"
        }
    ]
}

0 个答案:

没有答案