Using Boto to connect to S3 with Python - python

I'm trying to access AWS using Boto, and it's not working. I've installed Boto, and the boto.cfg in /etc. Here's my code:
import requests, json
import datetime
import hashlib
import boto
conn = boto.connect_s3()
Here's the error:
Traceback (most recent call last):
File "boto.py", line 4, in <module>
import boto
File "/home/mydir/public_html/boto.py", line 6, in <module>
conn = boto.connect_s3()
AttributeError: 'module' object has no attribute 'connect_s3'
What the hell? This isn't complicated.

It looks like the file you're working on is called boto.py. I think what's happening here is that your file is importing itself--Python looks for modules in the directory containing the file doing the import before it looks on your PYTHONPATH. Try changing the name to something else.

Use the Connection classes.
e.g.
from boto.s3.connection import S3Connection
from boto.sns.connection import SNSConnection
from boto.ses.connection import SESConnection
def connect_s3(self):
return S3Connection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)
def connect_sns(self):
return SNSConnection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)
def connect_ses(self):
return SESConnection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)

#valdogg21
I am following your instructions and put this into my code:
from boto.s3.connection import S3Connection
conn = S3Connection('<aws access key>', '<aws secret key>')
But despite my good intentions, it results in a small error. I just did
sudo pip install boto --upgrade to ensure I have the latest version installed.
This is the error message. Just wondering if I am a lone wolf or if others encounter this issue...
from boto.s3.connection import S3Connection ImportError: cannot import
name S3Connection

You may need to do something similar to how I had to utilize the EC2Connection class in some of my code, which looks like this:
from boto.ec2.connection import EC2Connection
conn = EC2Connection(...)
Also, from their docs (http://boto.s3.amazonaws.com/s3_tut.html):
>>> from boto.s3.connection import S3Connection
>>> conn = S3Connection('<aws access key>', '<aws secret key>')
EDIT: I know that doc page has the shortcut function you're trying to use, but I saw a similar problem when trying to do the same type of shortcut with EC2.

I have tried all of your solutions, but none of them seem to work. I keep going over StackOverFlow as I cannot see anyone else not having this rather small issue. Kind of weird fact is that in the server it works like a charm. The issue is on my Mac

I had this issue and was facing the same error when using boto3 and moto to mock s3 bucket.
boto3.connect_s3()
I switched back my library to boto and it worked fine. It looks like boto3 has migrated connect_s3() to resources():
boto.connect_s3() //works
boto3.resources('s3') //works
I could resolve similar issue for AWS Lambda too:
boto.connect_awslambda() //works
boto3.client('lambda') //works

Related

Python caldav Attribute Error

I just installed caldav 0.5.0 using pip on Windows. I tried to use this code from the documentation:
from datetime import datetime
import caldav
from caldav.elements import dav, cdav
# Caldav url
url = "https://user:pass#hostname/caldav.php/"
client = caldav.DAVClient(url)
But I get this error:
AttributeError: module 'caldav' has no attribute 'DAVClient'
Does someone know what causes this issue?
It is because your file is named calendar.py, which causes some kind of collision somewhere. Renaming your file to something else will do the trick (it did for me).

Python : no JSON object could be decoded

I am trying to run this app:
https://github.com/bmjr/guhTrends
I have python 2.7.x running the following script at command line. I reckon it was written using python3.x. What is deprecated in the code below?
import urllib
import json
import matplotlib.pyplot as plt
dates = urllib.request.urlopen('http://charts.spotify.com/api/tracks/most_streamed/global/weekly/')
dataDates = json.loads(dates.read().decode())
the error:
Traceback (most recent call last):
File "DataMining.py", line 6, in <module>
dates = urllib.request.urlopen('http://charts.spotify.com/api/tracks/most_streamed/global/weekly/')
AttributeError: 'module' object has no attribute 'request'
That script won't work under python2 as urllib of python2 has no request module.
Use urllib2.urlopen instead of urllib.request if you want start running that script under python2 .
To get python script work on bith (python2 and python3) use six module which is Python 2 and 3 Compatibility Library.
from six.moves import urllib
import json
import matplotlib.pyplot as plt
dates = urllib.request.urlopen('http://charts.spotify.com/api/tracks/most_streamed/global/weekly/')
dataDates = json.loads(dates.read().decode())
You're requesting a resource that is not currently available (I'm seeing a 504). Since this could potentially happen any time you request a remote service, always check the status code on the response; it's not that your code is necessarily wrong, in this case it's that you're assuming the response is valid JSON without checking whether the request was successful.
Check the urllib documentation to see how to do this (or, preferably, follow the recommendation at the top of that page and use the requests package instead).

AttributeError: 'BlockBlobService' object has no attribute 'create_block_blob_from_path'

Accocrding this manual I shoud use thise pice of code:
from azure.storage.blob import ContentSettings
block_blob_service.create_block_blob_from_path(
'mycontainer',
'myblockblob',
'sunset.png',
content_settings=ContentSettings(content_type='image/png')
)
But got this error:
AttributeError: 'BlockBlobService' object has no attribute 'create_block_blob_from_path'
Tried from git, as well as from pip
pip install azure-storage
I believe the tutorial is out of date, compared with the latest Python SDK. I don't think there's a create_block_blob_from_path anymore - I looked at the sdk code (here). There are separate imports for block blobs and page blobs, with the method being create_blob_from_path.
So with a simple correction:
from azure.storage.blob import BlockBlobService
from azure.storage.file import ContentSettings
blob_service = BlockBlobService(account_name="<storagename>",account_key="<storagekey>")
content_settings = ContentSettings(content_type = "image/png")
blob_service.create_blob_from_path("mycontainer","myblockblob","sunset.png",content_settings)

Import modules using RPYC

I'm trying to remote into an interactive shell and import modules within python 2.7. I'm getting hung up. So far this is what I've got:
import rpyc
import socket
hostname = socket.gethostname()
port = 12345
connections = rpyc.connect(hostname,port)
session = connections.root.getSession()
session exists
>>>session
<blah object at 0xMore-Goop>
I want to issue an import sys so I can add another module to the path. However when I try to see if modules exist in the path I get the following:
>>>connections.modules
AttributeError: 'Connection' object has no attribute 'modules'
What I need to execute remotely is the following:
import sys
sys.path.append(path/to/import)
import file
log = file.logger(session, path/to/log)
Is it possible to have rpyc issue the above content? Thanks in advance
You can add the following methods to the service:
import sys, importlib, rpyc
...
class MyService(rpyc.Service):
...
def exposed_import_module(self, mod):
return importlib.import_module(mod)
def exposed_add_to_syspath(self, path):
return sys.path.append(path)
and access it like this:
connections.root.add_to_syspath('path/to/import')
file = connections.root.import_module('file')
file.logger(session, 'path/to/log')

Boto Syntax Error?

I am importing boto.dynamodb.table and getting a syntax error. I don't see how it is related to what I'm doing. I haven't implemented / used it and yet upon launching it finds a syntax error.
The error from my console looks like this:
File "api.py", line 10, in <module>
import dynamoAccess
File "/Users/tai/Documents/workspace/testSelenium/testS/dynamoAccess.py", line 6, in <module>
from boto.dynamodb2.table import Table
File "/Library/Python/2.7/site-packages/boto/dynamodb2/table.py", line 3, in <module>
from boto.dynamodb2.fields import (HashKey, RangeKey,
File "/Library/Python/2.7/site-packages/boto/dynamodb2/fields.py", line 1, in <module>
from boto.dynamodb2.types import STRING
File "/Library/Python/2.7/site-packages/boto/dynamodb2/types.py", line 4, in <module>
from boto.dynamodb.types import Dynamizer
File "/Library/Python/2.7/site-packages/boto/dynamodb/types.py", line 112
]
^
SyntaxError: invalid syntax
The code that I believe this is related to is the first few lines (aka the dynamo table import) of dynamoAccess:
This is what I have:
import cleaner
import datetime
import awsAccess
import boto
from boto import dynamodb2
from boto.dynamodb2.table import Table
#create a connection to amazon s3
#aws_access_key_id=getenv('AWS_ACCESS_KEY');
#aws_secret_access_key=getenv('AWS_SECRET_KEY');
#aws_dynamo_region=getenv('DYANAMO_REGION')
#for running in pydev
aws_access_key_id=awsAccess.aws_access_key_id
aws_secret_access_key=awsAccess.aws_secret_access_key
aws_dynamo_region=awsAccess.aws_dynamo_region
decompiled_dynamo_table="decompiled_swfs"
text_dynamo_table="decompiled_swf_text"
image_dynamo_table="images_decompiled"
_dynamo_table="decompiled_swf_text"
Has anyone encountered this? I haven't modified the boto file.
Edit:
resinstalled boto but still getting the error:
Name: boto
Version: 2.31.1
Edit 2: Solved see answer below. Boto had a bug
Fixed - I replaced the boto dynamodb/types.py file with the one in github: https://github.com/boto/boto
There was a ] out of place that needed to be fixed. This was already fixed in the github version but apparently not yet pushed to pip
I believe that this may occur for other people due to the above error. If you encounter this simply update your file from github (or edit it yourself)

Categories

Resources