aws-sdk-cpp 테스트 프로그램

Dev-AWS/SDK(CPP) 2016. 1. 12. 13:05
aws-sdk-cpp를 이용해서 AWS S3에 파일을 업로드,다운로드,삭제 를 수행하는 테스트 프로그램입니다.
main.cpp - WS_BUCKET_NAME에 테스트할 S3 bucket 이름 적으셔야 됩니다.

!- 퍼가시는 것은 상관 없으나, 출처를 명시하셔야 합니다. -!


main.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <fstream>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/core/utils/memory/stl/AWSStringStream.h>
#include <aws/core/utils/HashingUtils.h>
#include <aws/core/utils/StringUtils.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <aws/s3/model/DeleteObjectRequest.h>
 
#define AWS_BUCKET_NAME "upload-test" #define AWS_OBJECT_NAME "test.txt" #define DOWNLOAD_FILE "downloadfile.txt"
 
const Aws::String AWS_ACCESS_KEY_ID = "<Enter your aws access key id>";
const Aws::String AWS_SECRET_ACCESS_KEY = "<Enter your aws secret access key>";
 
using namespace std;
 
int s1_file_upload();
int s1_file_download();
int s1_file_remove();
 
 
int main(int argc, char** argv)
{
    int ret;
 
    cout << "Test : s3 file upload" << endl << "- ";
    ret = s1_file_upload();
    if (ret)
        cout << "Success" << endl;
    else
        cout << "Failure" << endl;
 
    cout << "Test : s3 file download" << endl << "- ";
    ret = s1_file_download();
    if (ret)
        cout << "Success" << endl;
    else
        cout << "Failure" << endl;
 
    cout << "Test : s3 file remove" << endl << "- ";
    ret = s1_file_remove();
    if (ret)
        cout << "Success" << endl;
    else
        cout << "Failure" << endl;
 
    return 0;
}
 
int s1_file_upload()
{
    Aws::Client::ClientConfiguration config;
    config.scheme = Aws::Http::Scheme::HTTPS;
    config.connectTimeoutMs = 30000;
    config.requestTimeoutMs = 30000;
    config.region = Aws::Region::AP_NORTHEAST_1;
 
    Aws::S3::S3Client s3Client(Aws::Auth::AWSCredentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY), config);
 
    std::shared_ptr<Aws::IOStream> objectStream = Aws::MakeShared<Aws::StringStream>("BucketAndObjectOperationTest");
    *objectStream << "AWS S3 Test - Hello World";
    objectStream->flush();
    
    Aws::S3::Model::PutObjectRequest putObjectRequest;
    putObjectRequest.SetBucket(AWS_BUCKET_NAME);
    putObjectRequest.SetKey(AWS_OBJECT_NAME);
    putObjectRequest.SetBody(objectStream);
    putObjectRequest.SetContentLength(static_cast<long>(putObjectRequest.GetBody()->tellp()));
    putObjectRequest.SetContentMD5(Aws::Utils::HashingUtils::Base64Encode(Aws::Utils::HashingUtils::CalculateMD5(*putObjectRequest.GetBody())));
    putObjectRequest.SetContentType("text/plain");
 
    auto putObjectOutcome = s3Client.PutObject(putObjectRequest);
    if (!putObjectOutcome.IsSuccess())
        return 0;
 
    return 1;
}
 
int s1_file_download()
{
    Aws::Client::ClientConfiguration config;
    config.scheme = Aws::Http::Scheme::HTTPS;
    config.connectTimeoutMs = 30000;
    config.requestTimeoutMs = 30000;
    config.region = Aws::Region::AP_NORTHEAST_1;
 
    Aws::S3::S3Client s3Client(Aws::Auth::AWSCredentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY), config);
    Aws::S3::Model::GetObjectRequest getObjectRequest;
    getObjectRequest.SetBucket(AWS_BUCKET_NAME);
    getObjectRequest.SetKey(AWS_OBJECT_NAME);
 
    auto getObjectOutcome = s3Client.GetObject(getObjectRequest);
    if (!getObjectOutcome.IsSuccess())
        return 0;
 
    std::ofstream ofs;
    ofs.open(DOWNLOAD_FILE, ofstream::out | ofstream::binary);
    ofs << getObjectOutcome.GetResult().GetBody().rdbuf();
    ofs.close();
  
    return 1;
}
 
int s1_file_remove()
{
    Aws::Client::ClientConfiguration config;
    config.scheme = Aws::Http::Scheme::HTTPS;
    config.connectTimeoutMs = 30000;
    config.requestTimeoutMs = 30000;
    config.region = Aws::Region::AP_NORTHEAST_1;
 
    Aws::S3::S3Client s3Client(Aws::Auth::AWSCredentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY), config);
    
    Aws::S3::Model::DeleteObjectRequest deleteObjectRequest;
    deleteObjectRequest.SetBucket(AWS_BUCKET_NAME);
    deleteObjectRequest.SetKey(AWS_OBJECT_NAME);
 
    auto deleteObjectOutcome = s3Client.DeleteObject(deleteObjectRequest);
    if (!deleteObjectOutcome.IsSuccess())
        return 0;
 
    return 1;
}
 
cs




Makefile


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# # Makefile for "s3_test" # CC=g++ STRIP=strip ############################# TARGET= s3_test MACRO= CFLAGS= -std=gnu++11 LIBDIR= -L/usr/local/lib/linux/intel64 INCDIR= -I/usr/local/include LIBS= -laws-cpp-sdk-core -laws-cpp-sdk-s3 OBJS= main.o SRCS := $(OBJECTS:.o=.cpp) all: $(TARGET) $(TARGET): $(OBJS) $(SRCS) $(CC) -o $(TARGET) $(LIBDIR) $(OBJS) $(LIBS) $(STRIP) -s $(TARGET) .cpp.o: $(CC) -o $(@D)/$(@F) $(CFLAGS) $(INCDIR) $(MACRO) -c $(@D)/$(<F) clean: rm -f $(OBJS) $(TARGET)

 
cs



: