Python과 Vue.js를 이용한 AWS S3 파일 업로드 기능 구현

2018. 9. 14. 22:22서버 프로그래밍

로컬의 파일을 서버에 업로드하고, 업로드된 파일을 다시 AWS S3에 저장한다.

기존에 Javascript(Node.js, AngularJS)로 이미 구현해서 사용을 하고 있었으나, 이번에는 Python(Flask, Vue.js)으로 만들어야 해서 처음부터 다시 삽질을 한다.


파일 업로드 기능 구현을 위해서 참고할 만한 서버는 python으로, 클라이언트는 Vue.js로 만든 좋은 예제가 있다.

https://github.com/ZulfiqarAkram/UploadFileUsingVueJs


Python에서 AWS S3를 연동하려면 boto3 라이브러리를 사용하면 된다.

https://github.com/boto/boto3

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-example-creating-buckets.html

Using Boto 3

To use Boto 3, you must first import it and tell it what service you are going to use:

import boto3

# Let's use Amazon S3
s3 = boto3.resource('s3')

Now that you have an s3 resource, you can make requests and process responses from the service. The following uses the buckets collection to print out all bucket names:

# Print out bucket names
for bucket in s3.buckets.all():
    print(bucket.name)

It's also easy to upload and download binary data. For example, the following uploads a new file to S3. It assumes that the bucket my-bucket already exists:

# Upload a new file
data = open('test.jpg', 'rb')
s3.Bucket('my-bucket').put_object(Key='test.jpg', Body=data)

Resources and Collections will be covered in more detail in the following sections, so don't worry if you do not completely understand the examples.