【问题标题】:Limiting amazon s3 downloads service限制亚马逊 s3 下载服务
【发布时间】:2016-09-13 15:10:26
【问题描述】:

我运行一项服务,用户可以使用 Amazon S3 公开上传和下载文件到我们的网站。上个月我们遇到了一个问题,用户上传的文件被疯狂下载,导致 170 TB 的带宽和巨额账单。

与 Amazon 交谈并在 StackOverflow 上搜索以确保不再发生这种情况的方法是下载 S3 日志并解析它们,然后从那里采取行动。

我们可以构建这样的脚本,但我想肯定有一些开源或第三方服务为此提供脚本或服务?

【问题讨论】:

  • 您确实应该在 S3 前面放置一个 CDN 以减少您的带宽费用。
  • 哇,根据我的估计,170T 的“输出”带宽将花费您近 14,000 美元 - AWS 在这方面与您合作吗?还是你只需要吃掉它?
  • @E.J.Brennan 账单大约是 5000 美元。我向亚马逊描述了这个问题以及我们将采取哪些措施来解决这个问题,他们退还了这笔钱。

标签: amazon-web-services amazon-s3 limit filesize


【解决方案1】:

怎么样:

  1. 为下载创建 CloudFront 分发版

  2. 设置当分布的 BytesDownloaded 指标超过您选择的每月限制时触发的 CloudWatch 警报

  3. 添加在触发警报时触发的通知(发送到您创建的 SNS 主题)

  4. 添加一个 Lambda 函数,该函数在向该主题发送通知时由 SNS 触发(当然,SNS 主题还应该订阅您的电子邮件,以便您收到包含警报的电子邮件)

  5. 在 Lambda 函数中编写代码,使用 AWS 开发工具包更新云端分发并将 enabled 值设置为 false

(您还可以创建一个通知,当警报状态变回正常时触发并触发重新启用分发的 lambda 函数)

【讨论】:

    【解决方案2】:

    我的解决方案是在我的帐户上设置帐单提醒。我大致知道我每个月应该花多少钱,并相应地设置警报 - 大致我将该金额除以 4​​(周),并将一系列计费警报设置为我的 1/4、1/2、3/4 和 1X估计支出。

    这不是停止下载的技术解决方案,但至少有人会收到通知,他们可以在它失控之前采取行动。

    【讨论】:

      【解决方案3】:

      您最好的方法是使用 AWS Cloudfront 分发您的 S3 内容并实施 AWS Web 应用程序防火墙 (WAF) 并实施 IP 阻止。

      因此,如果 IP 访问您的 Cloud Front Distribution 超过 5 次,AWS WAF 将阻止该 IP。

      这里是详细指南。

      https://blogs.aws.amazon.com/security/post/Tx1ZTM4DT0HRH0K/How-to-Configure-Rate-Based-Blacklisting-with-AWS-WAF-and-AWS-Lambda

      【讨论】:

        【解决方案4】:

        我们很久以前就有类似的要求。

        我们使用 CloudTrail 日志来确定在我们的 AWS 账户上执行的所有活动。

        希望下载和过滤 Cloudtrail 日志的脚本对您有所帮助。 (以下脚本仅用于确定启动的instance-ids、owner、eventname,请根据需要修改)

        import boto3
        import gzip
        import os
        import json
        
        client = boto3.client('s3')
        bucketname = "mybucketname"
        list_bucket_objects = client.list_objects(Bucket=bucketname )
        download_path = '/home/ec2-user/cloudtrail/'
        
        # DOWNLOADING: Downloading Log files from S3
        for object in list_bucket_objects['Contents']:
           print object['Key']
           object_name = object['Key'].split('/')
           if len(object_name)==8:
              print "Downloading --->%s" % object_name[7]
              client.download_file(bucketname, object['Key'], download_path+object_name[7])
        
        # UNZIPPING: Unzipping the files in one folder    
        file_path = '/home/ec2-user/cloudtrail/'
        new_file_path = '/home/ec2-user/cloudtrail/logs/'
        
        #Create Log Directory
        if not os.path.exists(new_file_path):
            os.mkdir(new_file_path)
        
        files = os.listdir(file_path)
        for file in files:
           boolean = os.path.isfile(file_path+file)
           if boolean == True:
              f = gzip.GzipFile(file_path+file, 'rb')
              s = f.read()
              f.close()
              split_file = file.split('.')
              log_path =  new_file_path+split_file[0]
              print log_path
              out = open(log_path, 'wb')
              out.write(s)
              out.close()
        
        # PARSING AND FILTERING: parsing output into json format, filtering output and writing it in result.txt file
              fin = open(log_path).read()
              content = json.loads(fin)
              for i in range(0, len(content['Records'])):
                 event = content['Records'][i]['eventName']
                 if 'userName' in content['Records'][i]['userIdentity']:
                    user = content['Records'][i]['userIdentity']['userName']
                 if 'responseElements' in content['Records'][i]:
                    res_ele = content['Records'][i]['responseElements']
                    if res_ele:
                       if 'instancesSet' in content['Records'][i]['responseElements']:
                          if 'items' in content['Records'][i]['responseElements']['instancesSet']:
                             instance_id = content['Records'][i]['responseElements']['instancesSet']['items'][0]['instanceId']
                 if (event == "RunInstances" and instance_id != ""):
                    open('result.txt', 'ab').write(event+": :"+user+": :"+instance_id+"\n")
        
        #result.txt is stored in your current working directory.
        

        【讨论】:

          猜你喜欢
          • 2014-06-04
          • 2011-08-25
          • 2018-01-22
          • 2010-10-20
          • 1970-01-01
          • 1970-01-01
          • 2012-08-23
          • 1970-01-01
          相关资源
          最近更新 更多