Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.

Commit 8ad0b28

Browse files
committed
Make this script compatible with both python 2 and python 3.
1 parent 7adca32 commit 8ad0b28

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

s3_sample.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
# For more information about bucket name restrictions, see:
3434
# http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html
3535
bucket_name = "python-sdk-sample-%s" % uuid.uuid4()
36-
print "Creating new bucket with name: " + bucket_name
36+
print("Creating new bucket with name: " + bucket_name)
3737
bucket = s3.create_bucket(bucket_name)
3838

3939
# Files in Amazon S3 are called "objects" and are stored in buckets. A specific
@@ -46,7 +46,7 @@
4646
k = Key(bucket)
4747
k.key = 'python_sample_key.txt'
4848

49-
print "Uploading some data to " + bucket_name + " with key: " + k.key
49+
print ("Uploading some data to " + bucket_name + " with key: " + k.key)
5050
k.set_contents_from_string('Hello World!')
5151

5252
# Fetch the key to show that we stored something. Key.generate_url will
@@ -57,17 +57,21 @@
5757
# http://boto.readthedocs.org/en/latest/ref/s3.html#boto.s3.key.Key.generate_url
5858
expires_in_seconds = 1800
5959

60-
print "Generating a public URL for the object we just uploaded. This URL will be active for %d seconds" % expires_in_seconds
61-
print
62-
print k.generate_url(expires_in_seconds)
63-
print
64-
raw_input("Press enter to delete both the object and the bucket...")
60+
print("Generating a public URL for the object we just uploaded. This URL will be active for %d seconds" % expires_in_seconds)
61+
print('')
62+
print(k.generate_url(expires_in_seconds))
63+
print('')
64+
65+
66+
try: input = raw_input
67+
except NameError: pass
68+
input("Press enter to delete both the object and the bucket...")
6569

6670
# Buckets cannot be deleted unless they're empty. Since we still have a
6771
# reference to the key (object), we can just delete it.
68-
print "Deleting the object."
72+
print("Deleting the object.")
6973
k.delete()
7074

7175
# Now that the bucket is empty, we can delete it.
72-
print "Deleting the bucket."
76+
print("Deleting the bucket.")
7377
s3.delete_bucket(bucket_name)

0 commit comments

Comments
 (0)