버전 관리 활성화 버킷의 객체 나열
버전 관리 활성화 버킷의 객체 나열 (Listing objects in a versioning-enabled bucket)
이 섹션은 버전 관리 활성화 버킷에서 객체 버전을 나열하는 방법의 예시를 제공해요. Amazon S3는 객체 버전 정보를 버킷과 연결된 versions 하위 리소스에 저장해요. 자세한 내용은 일반 용도 버킷 구성 옵션을 참고하세요.
버전 관리 활성화 버킷의 객체를 나열하려면 ListBucketVersions 권한이 필요해요.
Amazon S3 콘솔을 사용해 객체의 다양한 버전을 보려면 다음 단계를 따라요.
본문
객체의 여러 버전 보기
- AWS Management Console에 로그인하고 https://console.aws.amazon.com/s3/ 에서 Amazon S3 콘솔을 열어요.
- Buckets 목록에서 객체가 포함된 버킷의 이름을 선택해요.
- 버킷의 객체 버전 목록을 보려면 Show versions 스위치를 선택해요.
- 각 객체 버전에 대해 콘솔은 고유한 버전 ID, 객체 버전이 생성된 날짜와 시간, 기타 속성을 표시해요. (버전 관리 상태를 설정하기 전에 버킷에 저장된 객체의 버전 ID는
null이에요.)
버전 없이 객체를 나열하려면 List versions 스위치를 선택해요. 콘솔의 객체 개요 창에서 객체 버전을 보고, 다운로드하고, 삭제할 수도 있어요. 자세한 내용은 Amazon S3 콘솔에서 객체 속성 보기를 참고하세요.
참고 300개가 넘는 객체 버전에 접근하려면 AWS CLI 또는 객체의 URL을 사용해야 해요.
중요 객체가 최신(현재) 버전으로 삭제된 경우에만 객체를 삭제 취소할 수 있어요. 삭제된 객체의 이전 버전은 삭제 취소할 수 없어요. 자세한 내용은 S3 버전 관리를 사용한 객체의 여러 버전 유지를 참고하세요.
이 섹션의 예시는 버전 관리 활성화 버킷에서 객체 목록을 검색하는 방법을 보여줘요. 더 낮은 값을 지정하지 않는 한 각 요청은 최대 1,000개의 버전을 반환해요. 버킷에 이 한도보다 많은 버전이 있으면 모든 버전 목록을 검색하기 위해 일련의 요청을 보내요. 결과를 "페이지" 단위로 반환하는 이 과정을 페이지 매김(pagination)이라고 해요.
페이지 매김 작동 방식을 보여주기 위해 예시는 각 응답을 두 개의 객체 버전으로 제한해요. 첫 번째 결과 페이지를 검색한 후 각 예시는 버전 목록이 잘렸는지 확인해요. 잘렸다면 예시는 모든 버전을 검색할 때까지 페이지 검색을 계속해요.
참고 다음 예시는 버전 관리가 활성화되지 않은 버킷이나 개별 버전이 없는 객체에서도 작동해요. 그런 경우 Amazon S3는 버전 ID가
null인 객체 목록을 반환해요.
다른 AWS SDK 사용에 대한 자세한 내용은 AWS Developer Center를 참고하세요.
Java
작동하는 샘플을 만들고 테스트하는 방법에 대한 지침은 AWS SDK for Java Developer Guide의 Getting Started를 참고하세요.
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ListVersionsRequest;
import com.amazonaws.services.s3.model.S3VersionSummary;
import com.amazonaws.services.s3.model.VersionListing;
public class ListKeysVersioningEnabledBucket
{
public static void main(String[] args)
{
Regions clientRegion = Regions.DEFAULT_REGION;
String bucketName = "*** Bucket name ***";
try
{
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new ProfileCredentialsProvider())
.withRegion(clientRegion)
.build();
// Retrieve the list of versions. If the bucket contains more versions
// than the specified maximum number of results, Amazon S3 returns
// one page of results per request.
ListVersionsRequest request = new ListVersionsRequest()
.withBucketName(bucketName)
.withMaxResults(2);
VersionListing versionListing = s3Client.listVersions(request);
int numVersions = 0, numPages = 0;
while (true)
{
numPages++;
for (S3VersionSummary objectSummary : versionListing.getVersionSummaries())
{
System.out.printf("Retrieved object %s, version %s\n",
objectSummary.getKey(),
objectSummary.getVersionId());
numVersions++;
}
// Check whether there are more pages of versions to retrieve. If
// there are, retrieve them. Otherwise, exit the loop.
if (versionListing.isTruncated())
{
versionListing = s3Client.listNextBatchOfVersions(versionListing);
} else
{
break;
}
}
System.out.println(numVersions + " object versions retrieved in " + numPages + " pages");
} catch (AmazonServiceException e)
{
// The call was transmitted successfully, but Amazon S3 couldn't process
// it, so it returned an error response.
e.printStackTrace();
} catch (SdkClientException e)
{
// Amazon S3 couldn't be contacted for a response, or the client
// couldn't parse the response from Amazon S3.
e.printStackTrace();
}
}
}
.NET
코드 예시를 설정하고 실행하는 방법에 대한 자세한 내용은 AWS SDK for .NET Developer Guide의 Getting Started with the AWS SDK for .NET을 참고하세요.
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Threading.Tasks;
namespace Amazon.DocSamples.S3
{
class ListObjectsVersioningEnabledBucketTest
{
static string bucketName = "*** bucket name ***";
// Specify your bucket region (an example region is shown).
private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
private static IAmazonS3 s3Client;
public static void Main(string[] args)
{
s3Client = new AmazonS3Client(bucketRegion);
GetObjectListWithAllVersionsAsync().Wait();
}
static async Task GetObjectListWithAllVersionsAsync()
{
try
{
ListVersionsRequest request = new ListVersionsRequest()
{
BucketName = bucketName,
// You can optionally specify key name prefix in the request
// if you want list of object versions of a specific object.
// For this example we limit response to return list of 2 versions.
MaxKeys = 2
};
do
{
ListVersionsResponse response = await s3Client.ListVersionsAsync(request);
// Process response.
foreach (S3ObjectVersion entry in response.Versions)
{
Console.WriteLine("key = {0} size = {1}",
entry.Key, entry.Size);
}
// If response is truncated, set the marker to get the next
// set of keys.
if (response.IsTruncated)
{
request.KeyMarker = response.NextKeyMarker;
request.VersionIdMarker = response.NextVersionIdMarker;
}
else
{
request = null;
}
} while (request != null);
}
catch (AmazonS3Exception e)
{
Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
}
}
}
}
예시 — 버킷의 모든 객체 버전 나열
버킷의 모든 객체의 모든 버전을 나열하려면 GET Bucket 요청에서 versions 하위 리소스를 사용해요. Amazon S3는 최대 1,000개의 객체를 검색할 수 있으며, 각 객체 버전은 객체로 완전히 집계돼요. 따라서 버킷에 두 개의 키(예: photo.gif와 picture.jpg)가 있고 첫 번째 키에 990개, 두 번째 키에 400개의 버전이 있다면, 단일 요청은 photo.gif의 모든 버전 990개와 picture.jpg의 가장 최근 버전 10개만 검색해요. Amazon S3는 객체 버전을 저장된 순서대로 반환하며, 가장 최근에 저장된 것이 먼저 반환돼요. GET Bucket 요청에 versions 하위 리소스를 포함해요.
GET /?versions HTTP/1.1
Host: bucketName.s3.amazonaws.com
Date: Wed, 28 Oct 2009 22:32:00 +0000
Authorization: AWS «r...RuU=
예시 — 키의 모든 버전 검색
객체 버전의 하위 집합을 검색하려면 GET Bucket 요청 파라미터를 사용해요. 자세한 내용은 GET Bucket을 참고하세요.
- 검색하려는 객체의 키로
prefix파라미터를 설정해요. versions하위 리소스와prefix를 사용해GET Bucket요청을 보내요.
GET /?versions&prefix=objectName HTTP/1.1
예시 — 접두사를 사용해 객체 검색
다음 예시는 키가 myObject이거나 myObject로 시작하는 객체를 검색해요.
GET /?versions&prefix=myObject HTTP/1.1
Host: bucket.s3.amazonaws.com
Date: Wed, 28 Oct 2009 22:32:00 GMT
Authorization: AWS «redac...RuU=
다른 요청 파라미터를 사용해 객체의 모든 버전 중 하위 집합을 검색할 수 있어요. 자세한 내용은 Amazon Simple Storage Service API Reference의 GET Bucket을 참고하세요.
예시 — 응답이 잘린 경우 추가 객체 목록 검색
GET 요청에서 반환될 수 있는 객체 수가 max-keys 값을 초과하면 응답에 <isTruncated>true</isTruncated>가 포함되고, 요청을 충족하지만 반환되지 않은 첫 번째 키(NextKeyMarker)와 첫 번째 버전 ID(NextVersionIdMarker)가 포함돼요. 반환된 값을 후속 요청의 시작 위치로 사용해 GET 요청을 충족하는 추가 객체를 검색해요.
다음 프로세스를 사용해 버킷에서 원래 GET Bucket versions 요청을 충족하는 추가 객체를 검색해요. key-marker, version-id-marker, NextKeyMarker, NextVersionIdMarker에 대한 자세한 내용은 Amazon Simple Storage Service API Reference의 GET Bucket을 참고하세요.
다음은 원래 GET 요청을 충족하는 추가 응답이에요.
key-marker값을 이전 응답의NextKeyMarker에 반환된 키로 설정해요.version-id-marker값을 이전 응답의NextVersionIdMarker에 반환된 버전 ID로 설정해요.key-marker와version-id-marker를 사용해GET Bucket versions요청을 보내요.
예시 — 지정된 키와 버전 ID로 시작하는 객체 검색
GET /?versions&key-marker=myObject&version-id-marker=298459348571 HTTP/1.1
Host: bucket.s3.amazonaws.com
Date: Wed, 28 Oct 2009 22:32:00 GMT
Authorization: AWS «redac...RuU=
다음 명령은 버킷에 있는 객체의 모든 버전에 대한 메타데이터를 반환해요.
aws s3api list-object-versions --bucket amzn-s3-demo-bucket1
list-object-versions에 대한 자세한 내용은 AWS CLI Command Reference의 list-object-versions를 참고하세요.
출처: 문서