【问题标题】:Accessing S3 using PEM for authentication with Python使用 PEM 访问 S3 以使用 Python 进行身份验证
【发布时间】:2019-05-17 17:37:05
【问题描述】:

我获得了一个 PEM 文件来访问 S3 存储桶。我可以使用 curl (例如 curl -v -k --cert file_path_in_bucket, -O https:ip/sourcefilepath )将文件复制到存储桶——很好地表明 PEM 允许我访问存储桶。

我尝试将其转换为Python Requests: .pem -> .crt + key 之后的 Python requests.get 调用,但得到 SSLError ("bad handshake: Error([('SSL routines', 'ssl3_get_server_certificate', 'certificate verify failed')],) )。在我看来,我需要提供更多信息。

非常感谢任何帮助。

【问题讨论】:

    标签: python ssl-certificate


    【解决方案1】:

    原来答案如下:

    r= requests.get('https://ip:port/file_path', verify=False, cert='pem_file_path')

    注意,请参考 cert 参数来传递 pem 文件。

    愉快的编码

    【讨论】:

      【解决方案2】:

      通过 Python 访问 S3 的首选方式是使用 BOTO3 库。

      假设您已经配置了 SECRET KEY 和 Credentials,编写起来非常简单..

      import boto3
      s3list = boto3.resource('s3')
      print('Displaying list of Buckets I have access to ')
      for bucket in s3list.buckets.all():
         print(bucket.name)
      

      【讨论】:

      • 我知道如何使用 ACCESS_KEY/ SECRET_KEY 访问存储桶。这里的重要元素是在这种情况下我必须使用 PEM 文件。感谢您愿意提供帮助!
      【解决方案3】:

      这是使用 Boto3 上传图片

      import boto3
      img = open('some.jpg','rb')
      s3list.Bucket('mybucket').put_object(Key='some.jpg', Body=img)
      

      【讨论】:

        【解决方案4】:

        正如他们已经向您建议的那样,boto3 易于使用。 这是我的函数,用于将对象放入/从 s3 存储桶中获取。

        def putdataons3(localfile, remotefile):
            try:
                bucket = 'yourbucket'
                boto3accesskey = 'your access key'
                boto3secretaccesskey = 'your secret access key'
                boto3client = boto3.client('s3', region_name='eu-central-1', aws_access_key_id=boto3accesskey, aws_secret_access_key=boto3secretaccesskey)
                d = boto3client.put_object(Body=open(localfile, 'rb').read(), Bucket=bucket, Key=remotefile)
                if d['ResponseMetadata']['HTTPStatusCode'] == 200:
                    return True
                return False
            except:
                return False
        
        def getdatafroms3(remotefile, localfile, overwrite=True):
            try:
                if overwrite == False:
                    if os.path.isfile(localfile):
                        return False
                bucket = 'yourbucket'
                boto3accesskey = 'your access key'
                boto3secretaccesskey = 'your secret access key'
                boto3client = boto3.client('s3', region_name='eu-central-1', aws_access_key_id=boto3accesskey, aws_secret_access_key=boto3secretaccesskey)
                d = boto3client.get_object(Bucket=bucket, Key=remotefile)
                if d['ResponseMetadata']['HTTPStatusCode'] == 200:
                    f = open(localfile, "wb")
                    d = d['Body'].read()
                    f.write(d)
                    f.close()
                    return True
                return False
            except:
                return False
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-06-21
          • 2014-02-11
          • 2014-11-12
          • 2023-03-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多