简体   繁体   English

最近AWS区域的客户端IP地址

[英]Client IP Address to Closest AWS Region

Question

I would like to upload some data to AWS from a client device, but I'd like to upload to the closest AWS Region's S3 Bucket. 我想从客户端设备上传一些数据到AWS,但我想上传到最近的AWS Region的S3 Bucket。

Similarly, I'd like to be able to download from the nearest region. 同样,我希望能够从最近的地区下载。

Of course, I'd set up a bucket in each region 当然,我会在每个地区设置一个水桶

Is there a system that I can use that maybe takes the IP Address of the client, then works out whether it's us-west-1, eu-west-1, eu-central-1, ap-northeast-1 etc? 是否有一个我可以使用的系统可能需要客户端的IP地址,然后确定它是us-west-1,eu-west-1,eu-central-1,ap-northeast-1等?

The crux of the problem is this. 问题的症结在于此。 The data i'm uploading is useful only to one person and it needs to get to that one person as quickly as possible. 我上传的数据仅对一个人有用,它需要尽快找到那个人。

So if I'm in England, I upload a file and my intended recipient is currently in Japan (as they could be on the move) - Uploading to Londons AWS region would have a higher ping time, than of a region closer to Japan. 因此,如果我在英格兰,我上传一个文件,我的目标收件人目前在日本(因为他们可能在移动) - 上传到伦敦AWS区域的ping时间将比靠近日本的区域更高。

Route53 latency based routing could help you determine the closest region. 基于Route53延迟的路由可以帮助您确定最近的区域。 However the bucket name will be different in each region, so I'm not sure how you would use this directly with S3. 但是每个区域的存储桶名称都不同,所以我不确定如何直接在S3中使用它。

I think the best option is to place a CloudFront distribution in front of a single S3 bucket. 我认为最好的选择是将CloudFront分配放在单个S3存储桶前面。 Then your users can automatically upload to the closest CloudFront edge location. 然后,您的用户可以自动上传到最近的CloudFront边缘位置。 https://aws.amazon.com/blogs/aws/amazon-cloudfront-content-uploads-post-put-other-methods/ https://aws.amazon.com/blogs/aws/amazon-cloudfront-content-uploads-post-put-other-methods/

Find client's location from IP 从IP查找客户端的位置

Use geoip 使用geoip

pip install python-geoip
pip install python-geoip-geolite2

Then your code will look something like this. 然后你的代码看起来像这样。

from geoip import geolite2
match = geolite2.lookup('8.8.8.8')

print match.location

This produces, (37.386, -122.0838) 这产生,(37.386,-122.0838)

Find locations of all AWS centers 查找所有AWS中心的位置

The information is available from: http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/LocationsOfEdgeServers.html you need to find the geolocations for them. 该信息可从以下网址获得: http//docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/LocationsOfEdgeServers.html您需要为它们找到地理位置。 That can be done with geopy 这可以用geopy完成

pip install geopy

Then 然后

from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.geocode("Singapore")
print location.latitude, location.longitude

Which gives 这使

1.2904527 103.852038

You need to do this for all your locations and save the data somehwere. 您需要为所有位置执行此操作并保存数据。 Possibly in an RDBMS (if you are doing that consider using django, django has excellent support for geolocation searching using GeoDjango) 可能在RDBMS中(如果你正在考虑使用django,django对使用GeoDjango进行地理定位搜索有很好的支持)

Finally finding the distance 终于找到了距离

Having found the client location, let us call it l1, and having found the data center locations, it's time to find the distance 找到客户端位置后,让我们将其称为l1,找到数据中心位置后,就可以找到距离了

from geopy.distance import great_circle
great_circle(l1.point, l2.point)

And there you have the distance 那里有距离

Finding the closest distance 找到最近的距离

You could loop through all your saved locations and find the closest distance, or if you saved your data in an RDBMS that supports geospatial data (postgis immidiately comes to mind) you can use the ST_Distance function to do the distance compaison quickly and effectively with very little code. 您可以循环浏览所有已保存的位置并找到最近的距离,或者如果您将数据保存在支持地理空间数据的RDBMS中(可以立即想到postgis),您可以使用ST_Distance函数快速有效地执行距离同步小代码。 As mentioned earlier, django has excellent support for geospatial queries. 如前所述, django对地理空间查询提供了出色的支持。

If you were to use Postgis/Django , the loop involving great_circle would be replaced by a call to st_distance. 如果您使用Postgis / Django,涉及great_circle的循环将被调用st_distance替换。

You could use the "Transfer acceleration" feature that S3 offers (You can enable it in the bucket properties using the AWS console). 您可以使用S3提供的“传输加速”功能(您可以使用AWS控制台在存储桶属性中启用它)。

Documentation: https://docs.aws.amazon.com/AmazonS3/latest/dev/transfer-acceleration.html 文档: https//docs.aws.amazon.com/AmazonS3/latest/dev/transfer-acceleration.html

You might want to use Transfer Acceleration on a bucket for various reasons, including the following: 您可能希望在存储桶上使用传输加速,原因有多种,包括:

  • You have customers that upload to a centralized bucket from all over the world. 您的客户可以从世界各地上传到集中式存储桶。
  • You transfer gigabytes to terabytes of data on a regular basis across continents. 您可以在各大洲定期将千兆字节转换为数TB。
  • You underutilize the available bandwidth over the Internet when uploading to Amazon S3. 上传到Amazon S3时,您未充分利用Internet上的可用带宽。

With Boto, you can read the region_name from the session.Session object: 使用Boto,您可以从session.Session对象中读取region_name:

my_session = boto3.session.Session()
my_region = my_session.region_name

The region_name is defined as session.get_config_variable('region') region_name定义为session.get_config_variable('region')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM