programing

강제 다운로드 대신 AWS S3 디스플레이 파일 인라인

projobs 2021. 1. 15. 07:28
반응형

강제 다운로드 대신 AWS S3 디스플레이 파일 인라인


어떤 이유로 내 S3 버킷의 파일이 인라인으로 표시되지 않고 강제로 다운로드되므로 이미지 링크를 복사하여 주소 표시 줄에 붙여 넣은 다음 탐색하면 브라우저에서 다운로드하도록 승격됩니다. 대신 URL로 이동하려면 이미지 열기를 클릭해야합니다.

S3에서 파일이 제공되는 방식을 변경하는 모든 방법


Content-Type을 변경해야합니다. S3 콘솔에서 객체를 마우스 오른쪽 버튼으로 클릭하고 속성을 선택한 다음 메타 데이터 아래에 있습니다. 프로그래밍 방식으로 수행 할 수도 있습니다 : http://docs.amazonwebservices.com/AWSSDKforPHP/latest/index.html#m=AmazonS3/change_content_type


$client->putObject(array(
        'Bucket'     => 'buckname',
        'Key'        => $destination,
        'SourceFile' => $source,
        'ContentType' =>'image/jpeg', //<-- this is what you need!
        'ACL'          => 'public-read'//<-- this makes it public so people can see it
    ));

첨부 대신 인라인에 대해서도 Content Disposition을 지정해야합니다.

$client->putObject(array(
    'Bucket'     => 'buckname',
    'Key'        => $destination,
    'SourceFile' => $source,
    'ContentType' =>'image/jpeg', //<-- this is what you need!
    'ContentDisposition' => 'inline; filename=filename.jpg', //<-- and this !
    'ACL'          => 'public-read'//<-- this makes it public so people can see it
));

파이썬을 사용하는 경우 다음과 같이 ContentType을 설정할 수 있습니다.

s3 = boto3.client('s3')

mimetype = 'image/jpeg' # you can programmatically get mimetype using the `mimetypes` module
s3.upload_file(
    Filename=local_path,
    Bucket=bucket,
    Key=remote_path,
    ExtraArgs={
        "ContentType": mimetype
    }
)

aws cli에서도 동일한 결과를 얻을 수 있습니다. 참고 s3 문서 *.jpeg확인

aws s3 cp \
      --exclude "*" \
      --include "*.jpeg" \
      --content-type="image/jpeg"  \
      --metadata-directive="REPLACE" \
      --recursive \
      --dryrun \
       s3://<bucket>/<path>/ \
       s3://<bucket>/<path>/

또는 한 번에 하나의 객체를 수정하려는 경우 s3api copy-object 를 사용할 수 있습니다.

aws s3api copy-object \
    --content-type="image/jpeg" \
    --metadata-directive="REPLACE" \
    --copy-source "<bucket>/<key>" \
    --bucket "<bucket>" \
    --key "<key>" \
    --acl public-read

참조 URL : https://stackoverflow.com/questions/14150854/aws-s3-display-file-inline-instead-of-force-download

반응형