미리 서명된 URL을 생성하여 S3 on Outposts 버킷에 객체 업로드
버킷 정책을 업데이트하지 않고 Outpost에 로컬로 저장된 객체에 한시적 액세스 권한을 부여하려면 미리 서명된 URL을 사용할 수 있습니다. 버킷 소유자는 미리 서명된 URL을 사용하여 Virtual Private Cloud(VPC)의 사용자와 객체를 공유하거나 이들에게 객체를 업로드 또는 삭제할 수 있는 권한을 부여할 수 있습니다.
AWS SDK 또는 AWS Command Line Interface(AWS CLI)를 사용하여 미리 서명된 URL을 생성하면 URL을 특정 작업과 연결합니다. 또한 최소 1초 및 최대 7일의 사용자 지정 만료 시간을 선택하여 미리 서명된 URL에 대한 한시적 액세스 권한을 부여할 수 있습니다. 미리 서명된 URL을 공유하면 VPC의 사용자가 원래 서명 사용자인 것처럼 URL에 포함된 작업을 수행할 수 있습니다. URL이 만료 시간에 도달하면 URL이 만료되고 더 이상 작동하지 않습니다.
미리 서명된 URL을 생성하면 보안 인증 정보를 제공한 후 다음을 지정해야 합니다.
미리 서명된 URL은 지정된 기간 동안만 유효합니다. 즉, 만료 날짜 및 시간 전에 URL에서 허용하는 작업을 시작해야 합니다. 미리 서명된 URL은 만료 날짜 및 시간까지 여러 번 사용할 수 있습니다. 임시 토큰을 사용하여 미리 서명된 URL을 생성할 경우, URL의 만료 시간이 토큰 만료 시간보다 이후인 경우에도 토큰이 만료되면 URL도 만료됩니다.
미리 서명된 URL에서 허용하는 작업이 멀티파트 업로드와 같이 여러 단계로 구성된 경우 만료 시간 전에 모든 단계를 시작해야 합니다. S3 on Outposts가 만료된 URL로 단계를 시작하려고 시도하는 경우 오류가 발생합니다.
미리 서명된 URL에 액세스할 수 있는 Virtual Private Cloud(VPC)의 사용자는 객체를 업로드할 수 있습니다. 예를 들어 미리 서명된 URL에 액세스할 수 있는 VPC의 사용자는 버킷에 객체를 업로드할 수 있습니다. 미리 서명된 URL은 미리 서명된 URL에 액세스할 수 있는 VPC의 모든 사용자에게 S3 on Outposts 버킷에 대한 액세스 권한을 부여하므로, URL을 적절하게 보호하는 것이 좋습니다. 미리 서명된 URL 보호에 대한 자세한 내용은 미리 서명된 URL 기능 제한 섹션을 참조하세요.
유효한 보안 자격 증명을 가진 사용자는 누구나 미리 서명된 URL을 만들 수 있습니다. 단, 미리 서명된 URL은 미리 서명된 URL에서 제공하려는 작업을 수행할 권한이 있는 사용자가 생성해야 합니다. 자세한 내용은 미리 서명된 URL을 생성할 수 있는 사용자 단원을 참조하십시오.
AWS SDK를 사용하여 S3 on Outposts 객체 작업에 대한 미리 서명된 URL 생성
- Java
-
- SDK for Java 2.x
-
이 예제는 한시적으로 S3 on Outposts 버킷에 객체를 업로드하는 데 사용할 수 있는 미리 서명된 URL을 생성하는 방법을 보여줍니다. 자세한 내용은 S3 on Outposts에서 미리 서명된 URL 사용 단원을 참조하십시오.
public static void signBucket(S3Presigner presigner, String outpostAccessPointArn, String keyName) {
try {
PutObjectRequest objectRequest = PutObjectRequest.builder()
.bucket(accessPointArn)
.key(keyName)
.contentType("text/plain")
.build();
PutObjectPresignRequest presignRequest = PutObjectPresignRequest.builder()
.signatureDuration(Duration.ofMinutes(10))
.putObjectRequest(objectRequest)
.build();
PresignedPutObjectRequest presignedRequest = presigner.presignPutObject(presignRequest);
String myURL = presignedRequest.url().toString();
System.out.println("Presigned URL to upload a file to: " +myURL);
System.out.println("Which HTTP method must be used when uploading a file: " +
presignedRequest.httpRequest().method());
// Upload content to the S3 on Outposts bucket by using this URL.
URL url = presignedRequest.url();
// Create the connection and use it to upload the new object by using the presigned URL.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type","text/plain");
connection.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("This text was uploaded as an object by using a presigned URL.");
out.close();
connection.getResponseCode();
System.out.println("HTTP response code is " + connection.getResponseCode());
} catch (S3Exception e) {
e.getStackTrace();
} catch (IOException e) {
e.getStackTrace();
}
}
- Python
-
- SDK for Python(Boto3)
-
이 예제는 한시적으로 S3 on Outposts 작업을 수행할 수 있는 미리 서명된 URL을 생성하는 방법을 보여줍니다. 자세한 내용은 S3 on Outposts에서 미리 서명된 URL 사용 단원을 참조하십시오. URL을 사용하여 요청하려면 Requests
패키지를 사용하세요.
import argparse
import logging
import boto3
from botocore.exceptions import ClientError
import requests
logger = logging.getLogger(__name__)
def generate_presigned_url(s3_client, client_method, method_parameters, expires_in):
"""
Generate a presigned S3 on Outposts URL that can be used to perform an action.
:param s3_client: A Boto3 Amazon S3 client.
:param client_method: The name of the client method that the URL performs.
:param method_parameters: The parameters of the specified client method.
:param expires_in: The number of seconds that the presigned URL is valid for.
:return: The presigned URL.
"""
try:
url = s3_client.generate_presigned_url(
ClientMethod=client_method,
Params=method_parameters,
ExpiresIn=expires_in
)
logger.info("Got presigned URL: %s", url)
except ClientError:
logger.exception(
"Couldn't get a presigned URL for client method '%s'.", client_method)
raise
return url
def usage_demo():
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
print('-'*88)
print("Welcome to the Amazon S3 on Outposts presigned URL demo.")
print('-'*88)
parser = argparse.ArgumentParser()
parser.add_argument('accessPointArn', help="The name of the S3 on Outposts access point ARN.")
parser.add_argument(
'key', help="For a GET operation, the key of the object in S3 on Outposts. For a "
"PUT operation, the name of a file to upload.")
parser.add_argument(
'action', choices=('get', 'put'), help="The action to perform.")
args = parser.parse_args()
s3_client = boto3.client('s3')
client_action = 'get_object' if args.action == 'get' else 'put_object'
url = generate_presigned_url(
s3_client, client_action, {'Bucket': args.accessPointArn, 'Key': args.key}, 1000)
print("Using the Requests package to send a request to the URL.")
response = None
if args.action == 'get':
response = requests.get(url)
elif args.action == 'put':
print("Putting data to the URL.")
try:
with open(args.key, 'r') as object_file:
object_text = object_file.read()
response = requests.put(url, data=object_text)
except FileNotFoundError:
print(f"Couldn't find {args.key}. For a PUT operation, the key must be the "
f"name of a file that exists on your computer.")
if response is not None:
print("Got response:")
print(f"Status: {response.status_code}")
print(response.text)
print('-'*88)
if __name__ == '__main__':
usage_demo()