2

I am trying to copy a file into an S3 bucket, using Python, like so:

cmd = 'aws s3 %s %s' % (filename, bucketname)
os.system(cmd)

It gives me a sh: 1: aws: not found error.

However, using s3cmd works just fine.

Why would s3cmd work, but not aws?

Also, I did which aws and it returned: /home/username/anaconda/bin/aws.

which s3cmd returns: /home/username/anaconda/bin/s3cmd.

Why does one work, but not the other, despite having the same root?

6
  • 2
    Try using the full path. Commented Jul 18, 2015 at 2:13
  • @jonnybazookatone: I'll try that. But why would one work, and not the other? My bashrc has the right path too.. Commented Jul 19, 2015 at 1:17
  • I'm not sure, it shouldn't matter. But it'll be interesting if that doesn't work, as it'll suggest some other underlying problem that isn't path related. Commented Jul 19, 2015 at 20:03
  • @jonnybazookatone: Will do. I'll update this question when I try it out and see what happens. Thanks! Commented Jul 19, 2015 at 20:08
  • @jonnybazookatone: It does work, but I'm still confused as to why it works! Commented Jul 20, 2015 at 17:24

3 Answers 3

3

This is because the AWSCLI is not installed in your system. I had also the same problem and I tried installing it with the pip package manager using pip install --user awscli but it didn't work. So I installed it directly in the system as follows:

curl "https://d1vvhvl2y92vvt.cloudfront.net/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

And the error was solved.

Sign up to request clarification or add additional context in comments.

Comments

2

A quick way to troubleshoot the issue is to try the full path on the OS call to see if it is a PATH problem:

cmd = '/path/to/aws s3 %s %s' % (filename, bucketname)
os.system(cmd)

There could be a few reasons why this is a problem, most likely related to the PATH variable (at first guess). However, it might be better to steer away from os.system as noted in the docs (https://docs.python.org/2/library/os.html#os.system) and use some alternative methods.

Using subprocess:

cmd = ['/path/to/aws', 's3', filename, bucketname]
subprocess.Popen(cmd)

Or just use the python AWS client boto3 package. There are many ways, but one quick example from this SO question (How to save S3 object to a file using boto3):

import boto3
s3_client = boto3.client('s3')
s3_client.upload_file(filename, bucketname, filename)

That one is not testable with moto, which can be annoying. Instead if you want to test, you can do something like this:

import boto3
s3_resource = boto3.resource('s3')

with open(filename, 'rb') as f:
    binary = f.read()

s3_resource.Bucket(bucketname).put_object(
    Key=filename,
    Body=binary
)

Comments

0

Install directly from the zip: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-linux.html#cliv2-linux-install

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.