AWS-从Lambda获取存储网关的激活密钥

时间:2019-01-10 15:49:07

标签: python-3.x amazon-web-services command-line aws-lambda

我正在尝试编写一些自动化操作,以从实例获取激活密钥并在AWS上激活存储网关。我一直在使用“ requests”库在适用于python 3.6的AWS Lambda中编写此代码,但只能使用以下代码:

Your web browser must have JavaScript enabled

每当我执行get请求时都会出错。但是在他们的文档中:

https://docs.aws.amazon.com/storagegateway/latest/userguide/get-activation-key.html#get-activation-key-cli

他们使用wget向实例发出请求,并获得带有激活密钥的响应。 Wget没有JavaScript解释器,那怎么办?

通过纯正的python是否可行,还是我坚持下载像selenium或phantomjs这样的胖二进制文件,将其提取为zip文件,然后上传该zip文件只是为了在其中使用javascript发出Web请求?

1 个答案:

答案 0 :(得分:0)

Curl安装在lambda上。我能够通过卷曲来实现这一点。

def get_gateway_activation_key(event):
  print("Getting activation key...")
  fgs = event["ResourceProperties"]["FileGatewayOptions"]["FileGatewaySettings"]
  url = "redirect_url=$(curl -f -s -S -w '%%{redirect_url}' \"http://%s/?activationRegion=%s\")  && echo $redirect_url" % (fgs["InstanceIP"],fgs["GatewayRegion"])
  redirect_url = os.popen(url).read()
  if redirect_url == "" or redirect_url is None:
      raise Exception("No redirect url returned for ip: %s" % fgs["InstanceIP"])
  key = redirect_url.split("activationKey=")[1].split("&gateway")[0]
  if key is None or key is "":
      raise Exception("Unable to extract the key from the returned redirect url: %s" %redirect_url)
  print("Retrieved Activation Key: %s" % key)
  return key