SemiColons
세미콜론
SemiColons
전체 방문자
오늘
어제
  • 분류 전체보기 (4)
    • TMI (1)
      • 일상 (1)
      • 운동 (0)
    • Languages (1)
      • JavaScript (0)
      • C# (1)
    • Server (1)
      • AWS (1)
    • Study (1)
      • Fastcampus (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • NET Core
  • 블로그하는이유
  • asp.net core
  • S3
  • aws s3
  • 조은
  • 파일업로드
  • 소개글
  • razor
  • filedownload
  • 블로그하기
  • 조은의 프런트엔드 실무가이드 리뷰
  • TheRed
  • Fastcmapus
  • AddControllersWithViews
  • 문해력
  • S3 파일업로드
  • 프런트엔드
  • fileinfo
  • kestrel

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
SemiColons

세미콜론

S3 - FileUpload, FileDownload, FileInfo
Server/AWS

S3 - FileUpload, FileDownload, FileInfo

2022. 7. 3. 18:43

최근 프로젝트를 하면서 사용하게 된 C# 기반 AWS S3 SDK 파일 관련 API 3가지 공유합니다.

아무래도 S3(CDN)이다 보니 FileDownload는 사용을 잘 안 하는 것 같기도 합니다.

그리고 'FileUpload시 해당 폴더가 없어도 알아서 폴더를 생성해주고 파일을 업로드' 해주는 게 정말 잘 만든 SDK인 것 같습니다.

AWS S3 SDK 설치

Visual Studio에서 Nuget으로 AWS S3 SDK를 다운로드해줍니다.

Using

using Amazon;
using Amazon.S3;
using Amazon.S3.IO;
using Amazon.S3.Transfer;

 

File Upload

1. 로컬 경로로 파일 업로드 방법

public async Task UploadFile(string fullPath, string UploadPath)
{
    string BucketName = "AWS에서 설정한 Bucket 이름";
    string AccessKey = "AWS에서 할당받은 AccessKey";
    string AccessScretKey = "AWS에서 할당받은 Access Scret Key";
    RegionEndpoint region = RegionEndpoint.APNortheast2; // AWS에서 셋팅된 서버위치
    
    // 위 region과 같은 서버위치 ( 서버위치를 string으로 받고 싶을때 )
    // RegionEndpoint region = RegionEndpoint.GetBySystemName("AP-North-east-2"); 
    
    using (TransferUtility fileTransferUtility = 
    		new TransferUtility(AccessKey, AccessSecretKey, region)
    {
    	// fullPath = "로컬 파일 경로 ( 파일명 포함 )"
        // UploadPath = "S3 업로드 할 경로 ( 경로구분은 '\' 역슬래쉬가 아닌 '/' 슬래쉬로 구분 )"
        // 폴더 경로 없으면 자동 폴더 생성
    	await fileTransferUtility.UploadAsync(fullPath, BucketName, UploadPath)
    }
}

 

2. 파일 스트림으로 파일 업로드 방법

public async Task UploadFileStream(byte[] FileBuffer, string UploadPath)
{
    string BucketName = "AWS에서 설정한 Bucket 이름";
    string AccessKey = "AWS에서 할당받은 AccessKey";
    string AccessScretKey = "AWS에서 할당받은 Access Scret Key";
    RegionEndpoint region = RegionEndpoint.APNortheast2; // AWS에서 셋팅된 서버위치
    
    // 위 region과 같은 서버위치 ( 서버위치를 string으로 받고 싶을때 )
    // RegionEndpoint region = RegionEndpoint.GetBySystemName("AP-North-east-2"); 
    
    using (TransferUtility fileTransferUtility = 
    		new TransferUtility(AccessKey, AccessSecretKey, region)
    {
    	using (MemoryStream ms = new MemoryStream(FileBuffer))
        {
            // fullPath = "로컬 파일 경로 ( 파일명 포함 )"
            // UploadPath = "S3 업로드 할 경로
            // ( 경로구분은 '\' 역슬래쉬가 아닌 '/' 슬래쉬로 구분 )"
            // 폴더 경로 없으면 자동 폴더 생성
            await fileTransferUtility.UploadAsync(fullPath, BucketName, UploadPath)
        }
    	
    }
}

 

File Download

1. 로컬 경로로 파일 다운로드 방법

파일 업로드 구간과 별로 다를 게 없습니다.

public async Task DownloadFile(string fullPath, string UploadPath)
{
    string BucketName = "AWS에서 설정한 Bucket 이름";
    string AccessKey = "AWS에서 할당받은 AccessKey";
    string AccessScretKey = "AWS에서 할당받은 Access Scret Key";
    RegionEndpoint region = RegionEndpoint.APNortheast2; // AWS에서 셋팅된 서버위치
    
    // 위 region과 같은 서버위치 ( 서버위치를 string으로 받고 싶을때 )
    // RegionEndpoint region = RegionEndpoint.GetBySystemName("AP-North-east-2"); 
    
    using (TransferUtility fileTransferUtility = 
    		new TransferUtility(AccessKey, AccessSecretKey, region)
    {
    	// fullPath = "로컬 파일 경로 ( 파일명 포함 )"
        // UploadPath = "S3 업로드 할 경로 ( 경로구분은 '\' 역슬래쉬가 아닌 '/' 슬래쉬로 구분 )"
    	await fileTransferUtility.DownloadAsync(fullPath, BucketName, UploadPath)
    }
}

 

File Info

C#에서 FileInfo의 기능과 동일하기 때문에 선언 방법만 알려드리겠습니다.

public async Task FileInfo(string UploadPath)
{
    string BucketName = "AWS에서 설정한 Bucket 이름";
    string AccessKey = "AWS에서 할당받은 AccessKey";
    string AccessScretKey = "AWS에서 할당받은 Access Scret Key";
    //RegionEndpoint region = RegionEndpoint.APNortheast2; // AWS에서 셋팅된 서버위치
    
    // 위 region과 같은 서버위치 ( 서버위치를 string으로 받고 싶을때 )
    RegionEndpoint region = RegionEndpoint.GetBySystemName("AP-North-east-2"); 
    
    using (AmazonS3Client client = 
    			new AmazoneS3Client(AccessKey, AccessSecretKey, region)
    {
        // UploadPath = "S3 업로드 할 경로 ( 경로구분은 '\' 역슬래쉬가 아닌 '/' 슬래쉬로 구분 )"
    	S3FileInfo fi = new S3FileInfo(client, BucketName, UploadPath);
    }
}

 

참고 사이트 - <참고>

 

* 잘못된 내용이 있으면 댓글로 피드백 부탁드립니다.

    SemiColons
    SemiColons

    티스토리툴바