In [157]:
import numpy as np
import urllib.request
import boto3

In [22]:
!pip3 install boto3


Collecting boto3
  Using cached boto3-1.4.4-py2.py3-none-any.whl
Collecting jmespath<1.0.0,>=0.7.1 (from boto3)
  Using cached jmespath-0.9.3-py2.py3-none-any.whl
Collecting s3transfer<0.2.0,>=0.1.10 (from boto3)
  Using cached s3transfer-0.1.10-py2.py3-none-any.whl
Collecting botocore<1.6.0,>=1.5.0 (from boto3)
  Using cached botocore-1.5.70-py2.py3-none-any.whl
Requirement already satisfied: docutils>=0.10 in /Users/Gon/anaconda3/lib/python3.5/site-packages (from botocore<1.6.0,>=1.5.0->boto3)
Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /Users/Gon/anaconda3/lib/python3.5/site-packages (from botocore<1.6.0,>=1.5.0->boto3)
Requirement already satisfied: six>=1.5 in /Users/Gon/anaconda3/lib/python3.5/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.6.0,>=1.5.0->boto3)
Installing collected packages: jmespath, botocore, s3transfer, boto3
Successfully installed boto3-1.4.4 botocore-1.5.70 jmespath-0.9.3 s3transfer-0.1.10

In [156]:
path = "wrong_img_id.txt"
with open(path, "r") as f:
    id_list = f.readlines()
img_id = []
for i in id_list:
    img_id.append(i.strip("\n"))
print("")




In [ ]:
with urllib.request.urlopen("http://www.python.org") as url:
    s = url.read()

In [64]:
# def save_img(path, img_url):
#     user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
#     user_agent = "Magic Browser"
#     headers={'User-Agent':user_agent,} 
    
#     f = open(path,'wb')
#     r=urllib.request.Request(img_url,None,headers)
#     f.write(urllib.request.urlopen(r).read())
#     f.close()
    
# for i in img_id[:2]:
#     path = "img_file2/" + i + ".jpg"
#     img_url = "https://s3.amazonaws.com/travel-with-friends/img_file/" + i +".jpg"
#     save_img(path, img_url)

In [133]:
# import boto3
# aws_access_key_id = "AKIAIW722D6CEHVQ4CEA"
# aws_secret_access_key = "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"
# region_name = "us-east-1"
# # conn = boto3.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
# session = boto3.Session(aws_access_key_id, aws_secret_access_key, region_name)
# bucket = session.resource("s3").Bucket("travel-with-friends")

# for key in bucket.list():
#     print (key.name.encode('utf-8'))

In [160]:
import os
from boto.s3.connection import S3Connection
from boto.s3.key import Key
aws_access_key_id =  "AKIAIZ3QOJ42ZZ7PVKCQ"
aws_secret_access_key =  "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"
conn = S3Connection(aws_access_key_id, aws_secret_access_key)
bucket = conn.get_bucket('travel-with-friends')  
#download from s3
# for i in bucket.list():
#     s3_img_name= str(i.key).replace("img_file/","").replace(".jpg","") #remove s3_path to just the name of the file
#     if s3_path in img_id:                                              #check if the file is in the target_list
#         i.get_contents_to_filename("img_file2/"+s3_img_name+".jpg")    #download+ save to path
    
#upload to s3
img_list= os.listdir("img_file/")
for count, img in enumerate(img_list):
    img_name = img
    path = 'img_file/' +img_name 
    s3_path = "img_file/" + img_name
    k = Key(bucket)
    k.key = s3_path
    k.set_contents_from_filename(path)
    
    if count%1000 ==0:
        print("already finish " + str(count) +" images")


already finish 0 images
already finish 1000 images
already finish 2000 images
already finish 3000 images
already finish 4000 images
already finish 5000 images
already finish 6000 images
already finish 7000 images
already finish 8000 images
already finish 9000 images
already finish 10000 images
already finish 11000 images
already finish 12000 images
already finish 13000 images
already finish 14000 images
already finish 15000 images
already finish 16000 images

In [144]:
k.set_contents_from_filename?

In [138]:
key_name = '0.jpg'
path = 'img_file2/' #Directory Under which file should get upload
full_key_name = os.path.join(path, key_name)
k = bucket.new_key(full_key_name)
k.set_contents_from_filename(key_name)


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-138-b9735a6bd4ce> in <module>()
      3 full_key_name = os.path.join(path, key_name)
      4 k = bucket.new_key(full_key_name)
----> 5 k.set_contents_from_filename(key_name)

/Users/Gon/anaconda3/envs/carnd-term1/lib/python3.5/site-packages/boto/s3/key.py in set_contents_from_filename(self, filename, headers, replace, cb, num_cb, policy, md5, reduced_redundancy, encrypt_key)
   1368         :return: The number of bytes written to the key.
   1369         """
-> 1370         with open(filename, 'rb') as fp:
   1371             return self.set_contents_from_file(fp, headers, replace, cb,
   1372                                                num_cb, policy, md5,

FileNotFoundError: [Errno 2] No such file or directory: '0.jpg'

In [37]:
# conn = conn.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
# bucket = conn.get_bucket(AWS_BUCKET_NAME)
# for file in bucket.list("FOLDER_NAME/", "/"):
#     <do something with required file>


Out[37]:
Session(region_name=None)

In [ ]:
# resource = session.resource('s3')
# bucket = resource.Bucket(bucket_name)

# for s3_key in self.client.list_objects(Bucket=self.bucket_name, Prefix=directory_path)['Contents']:
#     s3_object = s3_key['Key']
#     if s3_object not in exclude_file_names:
#         bucket.download_file(file_path, download_path + str(s3_object.split('/')[-1])

In [ ]:
# for bucket in s3.buckets.all():
#     print(bucket.name)
# # Upload a new file
# data = open('test.jpg', 'rb')
# s3.Bucket('my-bucket').put_object(Key='test.jpg', Body=data)

In [70]:
import cv2

In [112]:
import math, cv2, numpy as np

# load up an image
img_path = "img_file/0.jpg"
img = cv2.imread(img_path)

aspect_len= int(100)
weight, height, depth = img.shape
ratio = float(height) / float(weight)

if ratio <= 1:
    new_w = aspect_len
    new_h = int(aspect_len*ratio)
else:
    new_w = int(aspect_len*ratio)
    new_h = aspect_len
print (new_w,new_h)
# new_h = int(math.sqrt(TARGET_PIXEL_AREA / ratio) + 0.5)
# new_w = int((new_h * ratio) + 0.5)

img2 = cv2.resize(img, (new_w,new_h))
save_path = "img_file2/0.jpg"
cv2.imwrite(save_path, img2)


177 100
Out[112]:
True

In [132]:
import os
img_list= os.listdir("img_file/")
for count, img in enumerate(img_list[1:]):
    img_process(img)
    if count%1000 ==0:
        print("already finish " + str(count) +" images")


already finish 0 images
already finish 1000 images
already finish 2000 images
already finish 3000 images
already finish 4000 images
already finish 5000 images
already finish 6000 images
already finish 7000 images
already finish 8000 images
already finish 9000 images
already finish 10000 images
already finish 11000 images
already finish 12000 images
already finish 13000 images
already finish 14000 images
already finish 15000 images
already finish 16000 images

In [131]:
def img_process(img_name):
    img_path = "img_file/"+img_name
    img = cv2.imread(img_path)
#     print(img.shape)
    aspect_len= int(100)
    height, weight, depth = img.shape
    ratio = float(height) / float(weight)
#     print(ratio)
    if ratio >= 1:
        new_w = aspect_len
        new_h = int(aspect_len*ratio)
    else:
        new_w = int(aspect_len/ratio)
        new_h = aspect_len

    # new_h = int(math.sqrt(TARGET_PIXEL_AREA / ratio) + 0.5)
    # new_w = int((new_h * ratio) + 0.5)
    
    img2 = cv2.resize(img, (new_w,new_h))
#     print(img2.shape)
    save_path = "img_file2/icon_"+str(img_name)
    cv2.imwrite(save_path, img2)

In [113]:
s3_connection = boto.connect_s3()
bucket = s3_connection.get_bucket('your bucket name')
key = boto.s3.key.Key(bucket, 'some_file.zip')
with open('some_file.zip') as f:
    key.send_file(f)


Out[113]:
(1440, 2560, 3)

In [ ]: