๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœ’๏ธ Amazon Web Services (AWS)

[AWS] AWS S3 Bucket ์† ํŒŒ์ผ Contents ์ฝ์–ด์˜ค๊ธฐ

by A Lim Han 2023. 9. 1.

๐Ÿณ AWS S3 Bucket ์† ํŒŒ์ผ Contents๋ฅผ ์ฝ์–ด์˜ค๊ธฐ ์œ„ํ•œ Python Script

1. pip ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด Boto3 ๋‹ค์šด๋กœ๋“œ

!pip install boto3

 

 

++  Boto3 ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด?

-->  https://alim11.tistory.com/398

 

[Data Processing] Boto3 SDK๋ฅผ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ์—…๋กœ๋“œ ์ž๋™ํ™” (Forward AWS S3 Bucket)

๐ŸŒธ Boto3 SDK๋ฅผ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ์—…๋กœ๋“œ ์ž๋™ํ™” (Forward AWS S3 Bucket) 1. pip ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ boto3 ์„ค์น˜ !pip install boto3 ++ boto3 ๋ž€? boto3๋Š” Amazon Web Services (AWS)์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ธฐ ์œ„ํ•œ ์˜คํ”ˆ ์†Œ์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

alim11.tistory.com

2. Boto3 import ํ›„ AWS ๊ณ„์ • ์ •๋ณด & ํŒŒ์ผ ๊ฒฝ๋กœ ์„ค์ •

import boto3

# AWS ๊ณ„์ • ์ •๋ณด ๋ฐ ๋ฒ„ํ‚ท ์ด๋ฆ„ ์„ค์ •
aws_access_key = 'AWS ์—‘์„ธ์Šค ํ‚ค์˜ ID'
aws_secret_key = 'AWS ์‹œํฌ๋ฆฟ ์—‘์„ธ์Šค ํ‚ค'
bucket_name = 'S3 ๋ฒ„ํ‚ท๋ช…'
file_key = 'ํŒŒ์ผ ๊ฒฝ๋กœ ๋ฐ ์ด๋ฆ„'  # ํŒŒ์ผ์˜ ๊ฒฝ๋กœ ๋ฐ ์ด๋ฆ„

 

3. AWS S3 ํด๋ผ์ด์–ธํŠธ ์ƒ์„ฑ

# AWS S3 ํด๋ผ์ด์–ธํŠธ ์ƒ์„ฑ
s3 = boto3.client(
    's3',
    aws_access_key_id=aws_access_key,
    aws_secret_access_key=aws_secret_key
)

 

4. ํŒŒ์ผ ๋‚ด์šฉ์„ ์ฝ์–ด์™€ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ ๊ตฌํ˜„

# ํŒŒ์ผ ์ฝ๊ธฐ
try:
    response = s3.get_object(Bucket=bucket_name, Key=file_key)
    content = response['Body'].read().decode('utf-8')  # ํŒŒ์ผ ๋‚ด์šฉ ์ฝ์–ด์˜ค๊ธฐ

    # ํŒŒ์ผ ๋‚ด์šฉ ์ถœ๋ ฅ
    print("File Contents:")
    print(content)

    # ์ฝ์–ด์˜จ ๊ฐ’์— ๋”ฐ๋ผ ์กฐ๊ฑด๋ฌธ ์‹คํ–‰
    value = int(content.strip())  # ๋ฌธ์ž์—ด๋กœ ์ฝ์–ด์˜จ ๊ฐ’์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜
    if value == 0:
        print("์ •์ƒ์ž…๋‹ˆ๋‹ค")
    else:
        print("ํ”ผ์‹ฑ์ž…๋‹ˆ๋‹ค")

except Exception as e:
    print("Error reading file:", e)

 

value ๊ฐ’ ์ถœ๋ ฅ๋˜๋Š” ๋ฉ”์‹œ์ง€
0 ์ •์ƒ์ž…๋‹ˆ๋‹ค
1 (0์ด ์•„๋‹Œ ๋ชจ๋“  ๊ฒฝ์šฐ) ํ”ผ์‹ฑ์ž…๋‹ˆ๋‹ค

 

 


 

๐Ÿณ ์‹ค์ œ ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ๊ฒฐ๊ณผ

import boto3

# AWS ๊ณ„์ • ์ •๋ณด ๋ฐ ๋ฒ„ํ‚ท ์ด๋ฆ„ ์„ค์ •
aws_access_key = 'AWS ์—‘์„ธ์Šค ํ‚ค ID'
aws_secret_key = 'AWS ์‹œํฌ๋ฆฟ ์—‘์„ธ์Šค ํ‚ค'
bucket_name = '๋ฒ„ํ‚ท ์ด๋ฆ„'
file_key = 'ํŒŒ์ผ ๊ฒฝ๋กœ ๋ฐ ์ด๋ฆ„'  # ํŒŒ์ผ์˜ ๊ฒฝ๋กœ ๋ฐ ์ด๋ฆ„

# AWS S3 ํด๋ผ์ด์–ธํŠธ ์ƒ์„ฑ
s3 = boto3.client(
    's3',
    aws_access_key_id=aws_access_key,
    aws_secret_access_key=aws_secret_key
)

# ํŒŒ์ผ ์ฝ๊ธฐ
try:
    response = s3.get_object(Bucket=bucket_name, Key=file_key)
    content = response['Body'].read().decode('utf-8')  # ํŒŒ์ผ ๋‚ด์šฉ ์ฝ์–ด์˜ค๊ธฐ

    # ํŒŒ์ผ ๋‚ด์šฉ ์ถœ๋ ฅ
    print("File Contents:")
    print(content)

    # ์ฝ์–ด์˜จ ๊ฐ’์— ๋”ฐ๋ผ ์กฐ๊ฑด๋ฌธ ์‹คํ–‰
    value = int(content.strip())  # ๋ฌธ์ž์—ด๋กœ ์ฝ์–ด์˜จ ๊ฐ’์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜
    if value == 0:
        print("์ •์ƒ์ž…๋‹ˆ๋‹ค")
    else:
        print("ํ”ผ์‹ฑ์ž…๋‹ˆ๋‹ค")

except Exception as e:
    print("Error reading file:", e)

 

 


 

++  AWS S3 Bucket ์œผ๋กœ์˜ ํŒŒ์ผ ์—…๋กœ๋”ฉ ๊ณผ์ •์ด ๊ถ๊ธˆํ•˜๋‹ค๋ฉด?

-->  https://alim11.tistory.com/398

 

[Data Processing] Boto3 SDK๋ฅผ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ์—…๋กœ๋“œ ์ž๋™ํ™” (Forward AWS S3 Bucket)

๐ŸŒธ Boto3 SDK๋ฅผ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ์—…๋กœ๋“œ ์ž๋™ํ™” (Forward AWS S3 Bucket) 1. pip ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ boto3 ์„ค์น˜ !pip install boto3 ++ boto3 ๋ž€? boto3๋Š” Amazon Web Services (AWS)์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ธฐ ์œ„ํ•œ ์˜คํ”ˆ ์†Œ์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

alim11.tistory.com