boto3 unknownservice "mturk" - python

I am trying to use boto3 for Amazon Mechanical Turk. I was trying to get the client using the following code:
import boto3
endpoint_url = 'https://mturk-requester.us-east-1.amazonaws.com'
aws_access_key_id = <aws_access_key_id>
aws_secret_access_key = <aws_secret_access_key>
region_name = 'us-east-1'
client = boto3.client('mturk',
aws_access_key_id = aws_access_key_id,
aws_secret_access_key = aws_secret_access_key,
region_name=region_name,
endpoint_url = endpoint_url
)
But I am getting the following error about UnknownService name:
botocore.exceptions.UnknownServiceError: Unknown service: 'mturk'. Valid service
names are: acm,..., xray
Why is 'mturk' not in this list? The code I am using is taken from mturk developer website.
Any suggestion is welcome! Thanks in advance!

Related

aws config file to connect to s3 client

This is the line of code I use to connect to my boto3 client.
s3_client = boto3.client('s3', aws_access_key_id = '<access-key>', aws_secret_access_key = '<secret-key>')
How can I modify it to adapt to config file credentials within ~/.aws/config. For example, if I have
[default]
aws_access_key_id = FOO
aws_secret_access_key = BAR
[recordings]
aws_access_key_id = ABC
aws_secret_access_key = DEF
How can I set my python code to use recordings?
This is the easiest way to achieve what you want:
session = boto3.Session(profile_name='recordings')
s3_client = session.client('S3')
Best, Stefan

AWS presigned URLS location constraint is incompatible for the region specific endpoint this request was sent to

I am using a lambda to create a pre-signed URL to download files that land in an S3 bucket -
the code works and I get a URL but when trying to access it I get
af-south-1 location constraint is incompatible for the region-specific endpoint this request was sent to.
both the bucket and the lambda are in the same region
I'm at a loss as to what is actually happening any ideas or solutions would be greatly appreciated.
my code is below
import json
import boto3
import boto3.session
def lambda_handler(event, context):
session = boto3.session.Session(region_name='af-south-1')
s3 = session.client('s3')
for record in event['Records']:
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
url = s3.generate_presigned_url(ClientMethod='get_object',
Params={'Bucket': bucket,
'Key': key}, ExpiresIn = 400)
print (url)```
Set an endpoint_url=https://s3.af-south-1.amazonaws.com while generating the s3_client
s3_client = session.client('s3',
region_name='af-south-1',
endpoint_url='https://s3.af-south-1.amazonaws.com')
Could you please try using the boto3 client directly rather than via the session, and generate the pre-signed url :
import boto3
import requests
# Get the service client.
s3 = boto3.client('s3',region_name='af-south-1')
# Generate the URL to get 'key-name' from 'bucket-name'
url = s3.generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': 'bucket-name',
'Key': 'key-name'
}
)
You could also have a look at these 1 & 2, which resembles the same issue.

Have a issue with request routed53omains API

There was a problem accessing the routedomains module API.
code:
#!/usr/bin/python3.6
#encoding: utf-8
import boto3
key_id='xxxxxxxx'
access_key='xxxxxxxx'
client = boto3.client(
'route53domains',
aws_access_key_id = key_id,
aws_secret_access_key = access_key,
region_name = 'us-east-1'
)
all_domains= client.list_domains(DomainName="mydomain.com")
print(all_domains)
Run result:
[]
I can confirm that there are more than one domainame under the account(key_id and access_key).
and given sufficient permissions:
AmazonRoute53DomainsFullAccess
AmazonRoute53FullAccess
But response is null.
And request Route53 modules api is not problem:
client = boto3.client(
'route53',
region_name = 'us-east-1',
aws_access_key_id = "xxxxxxxxxxxx",
aws_secret_access_key = "xxxxxxxxxxxx"
)
client.get_paginator('list_hosted_zones')
My other account both Route53 and Route53domain request is work ok !
why? What's the problem? please help me, thanks!
The actual syntax for Route53domains client operation to list all the domains is:
response = client.list_domains(
Marker='string',
MaxItems=123
)
If the number of domains that are associated with the current AWS account is greater than the value that you specified for MaxItems , you can use Marker to return additional domains.
Refer to the boto3 documentation for Route53Domains
thank for your answer.
#!/usr/bin/python3.6
#encoding: utf-8
import boto3
key_id='xxxxxxxx'
access_key='xxxxxxxx'
client = boto3.client(
'route53domains',
aws_access_key_id = key_id,
aws_secret_access_key = access_key,
region_name = 'us-east-1'
)
all_domains= client.list_domains(MaxItems=123)
print(all_domains)
run result:
{'Domains': [], 'ResponseMetadata': {'RequestId': '2aa6e538-66e3-11e8-a89f-273ad6c882c8', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '2aa6e538-66e3-11e8-a89f-273ad6c882c8', 'content-type': 'application/x-amz-json-1.1', 'content-length': '14', 'date': 'Sun, 03 Jun 2018 04:04:09 GMT'}, 'RetryAttempts': 0}}
Domains is []
Sorry, I originally wanted to ask get_domain_detail() function:
Client.get_domain_detail(DomainName="mytest.com")
but found use that list_domains doesn't even have a domainname,
so,written error。
And I use the CMD API tool is still the same result:
./aws route53domains list-domains --max-items 123
{
"Domains": []
}
This may be an aws account or background issue. I don't know why.

Generate S3 pre-signed URL with v4 signature using python boto3

The following does not work:
From the boto 3 docs:
http://boto3.readthedocs.io/en/latest/guide/s3.html#generating-presigned-urls
This is my script with placeholder bucket and key values:
import boto3
import requests
from botocore.client import Config
# Get the service client.
s3 = boto3.client('s3', config=Config(signature_version='s3v4'))
# Generate the URL to get 'key-name' from 'bucket-name'
url = s3.generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': 'mybucketname',
'Key': 'myObject.txt'
}
)
print url
response = requests.get(url)
print response
S3 responds with a 403:
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>B5681E888657E2A1</RequestId>
<HostId>
FMS7oPPOXt4I0KXPPQwdBx2fyxze+ussMmy/BOWLVFusWMoU2zAErE08ez34O6VhSYRvIYFm7Bs=
</HostId>
</Error>
You need to provide aws credentials with your boto3 client. Docs here
If you need help getting access to your credentials on aws you can look here.
import boto3
client = boto3.client(
's3',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
aws_session_token=SESSION_TOKEN,
)
# Or via the Session
session = boto3.Session(
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
aws_session_token=SESSION_TOKEN,
)

Boto region_name missing

I have tried various attempts to estalish a connection to an S3 compatible service but I keep getting an error. The following code throws the error below. Any ideas?
import os
import boto
import boto.s3.connection
from boto.s3.key import Key
try:
conn = boto.s3.connect_to_region(region = 'nil',
aws_access_key_id = 'xx',
aws_secret_access_key = 'xx',
host = 'ds41s3-scs.xx.com',
calling_format = boto.s3.connection.OrdinaryCallingFormat(),
)
response = s3.list_buckets()
except Exception,e:
print str(e)
print "error"
The error:
TypeError: connect_to_region() missing 1 required positional argument: 'region_name'
Why is the region set to nil? The signature of connect_to_region is:
boto.s3.connect_to_region(region_name, **kw_params)
You are missing the mandatory argument: region_name
Go for latest version of AWS SDK for python (boto3) which supports latest features
import boto3
client = boto3.client('s3')
response = client.list_buckets()

Categories

Resources