Python/Python ftplib
Python ftplib : storbinary (upload file to s3) & delete (delete file)
CosmosProject
2021. 2. 2. 21:55
728x90
반응형
import ftplib
s3_host = '11.111.111.111'
s3_user_id = 'user_1'
s3_user_password = 'pw_1'
ftp = ftplib.FTP()
ftp.encoding = 'utf-8'
ftp.connect(s3_host) # connect to s3
ftp.login(s3_user_id, s3_user_password) # login to s3
csv_to_upload = 'test.csv'
file = open(csv_to_upload, 'rb')
ftp.storbinary('STOR '+ csv_to_upload, file)
print('File list :', ftp.nlst())
file.close()
ftp.quit() # quit s3
- Output
File list : [test.csv]
s3에 file을 upload하려면 위처럼 storbinary를 사용합니다.
이때 'STOR filename'이라는 명령어를 사용합니다.
import ftplib
s3_host = '11.111.111.111'
s3_user_id = 'user_1'
s3_user_password = 'pw_1'
ftp = ftplib.FTP()
ftp.encoding = 'utf-8'
ftp.connect(s3_host) # connect to s3
ftp.login(s3_user_id, s3_user_password) # login to s3
csv_to_upload = 'test.csv'
file = open(csv_to_upload, 'rb')
ftp.storbinary('STOR '+ csv_to_upload, file)
print('File list :', ftp.nlst())
try:
ftp.delete(target_s3_directory +'test.csv')
print('File is deleted.')
except Exception as e:
print(e)
file.close()
ftp.quit() # quit s3
- Output
File list : []
File is deleted.
delete는 s3에 존재하는 file을 삭제합니다. (directory는 삭제하지 못합니다. directory 삭제 시 rmd를 사용해야합니다.)
728x90
반응형