napi_wdossclient.cpp
6.47 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// Created on 2024/4/24.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".
#include "napi/native_api.h"
#include "hilog/log.h"
#include "napi/native_api.h"
#include <iostream>
#include <sstream>
#include "napi_util.h"
// #include <alibabacloud/oss/Export.h>
#include "alibabacloud/oss/OssClient.h"
#include <alibabacloud/oss/client/ClientConfiguration.h>
#include <alibabacloud/oss/auth/CredentialsProvider.h>
using namespace AlibabaCloud::OSS;
static napi_value WDOssClient_InitSdk(napi_env env, napi_callback_info info) {
AlibabaCloud::OSS::InitializeSdk();
return nullptr;
}
static napi_value WDOssClient_UnInitSdk(napi_env env, napi_callback_info info) {
AlibabaCloud::OSS::ShutdownSdk();
return nullptr;
}
static napi_value WDOssClient_IsSdkInitialized(napi_env env, napi_callback_info info) {
napi_value result;
bool isSdkInit = false;
isSdkInit = AlibabaCloud::OSS::IsSdkInitialized();
napi_get_boolean(env, isSdkInit, &result);
return result;
}
static napi_value WDOssClient_UploadFile(napi_env env, napi_callback_info info) {
napi_value result;
bool isSuccess = false;
napi_value IsInitResult;
bool bIsInit = false;
IsInitResult = WDOssClient_IsSdkInitialized(env,info);
napi_get_value_bool(env,IsInitResult,&bIsInit);
if(!bIsInit)
{
AlibabaCloud::OSS::InitializeSdk();
}
size_t argc = 7;
napi_value args[6] = {nullptr};
napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);
napi_valuetype valuetype0;
napi_typeof(env, args[0], &valuetype0);
napi_valuetype valuetype1;
napi_typeof(env, args[1], &valuetype1);
napi_valuetype valuetype2;
napi_typeof(env, args[2], &valuetype2);
napi_valuetype valuetype3;
napi_typeof(env, args[3], &valuetype3);
napi_valuetype valuetype4;
napi_typeof(env, args[4], &valuetype4);
napi_valuetype valuetype5;
napi_typeof(env, args[5], &valuetype5);
napi_valuetype valuetype6;
napi_typeof(env, args[6], &valuetype6);
std::string _Endpoint;
_Endpoint = utf8StringFromValue(env, args[0]);
std::string _accessKeySecret;
_accessKeySecret = utf8StringFromValue(env, args[1]);
std::string _accessKeyId;
_accessKeyId = utf8StringFromValue(env, args[2]);
std::string _sessionToken;
_sessionToken = utf8StringFromValue(env, args[3]);
std::string _BucketName;
_BucketName = utf8StringFromValue(env, args[4]);
std::string _ObjectName;
_ObjectName = utf8StringFromValue(env, args[5]);
std::string _localFileName;
_localFileName = utf8StringFromValue(env, args[6]);
if(_Endpoint.empty() || _accessKeySecret.empty() || _sessionToken.empty() || _accessKeyId.empty() ||
_BucketName.empty() || _ObjectName.empty() || _localFileName.empty())
{
isSuccess = false;
napi_get_boolean(env, isSuccess, &result);
AlibabaCloud::OSS::ShutdownSdk();
return result;
}
/* yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* 填写Bucket名称,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 填写文件完整路径,例如exampledir/exampleobject.txt。文件完整路径中不能包含Bucket名称。*/
std::string ObjectName = "exampledir/exampleobject.txt";
//本地文件名称
std::string localFileName = "D:\\localpath\\examplefile.txt";
ClientConfiguration conf;
std::string accessKeyId = "1111111111";
std::string accessKeySecret = "222222222";
std::string sessionToken = "333333333";
if(!_Endpoint.empty()){
Endpoint = _Endpoint;
}
if(!_accessKeySecret.empty()){
accessKeySecret = _accessKeySecret;
}
if(!_accessKeyId.empty()){
accessKeyId = _accessKeyId;
}
if(!_sessionToken.empty()){
sessionToken = _sessionToken;
}
if(!_BucketName.empty()){
BucketName = _BucketName;
}
if(!_ObjectName.empty()){
ObjectName = _ObjectName;
}
if(!_localFileName.empty()){
localFileName = _localFileName;
}
// auto credentialsProvider = std::make_shared<Credentials>(accessKeyId,accessKeySecret,sessionToken);
// auto client = std::make_shared<OssClient>(Endpoint, credentialsProvider, &conf);
auto client = std::make_shared<OssClient>(Endpoint, accessKeyId,accessKeySecret,sessionToken,conf);
/* 上传文件。*/
/* 填写本地文件完整路径,例如D:\\localpath\\examplefile.txt,其中localpath为本地文件examplefile.txt所在本地路径。*/
auto outcome = client->PutObject(BucketName, ObjectName,localFileName);
if (!outcome.isSuccess()) {
/* 异常处理。*/
std::cout << "PutObject fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
isSuccess = false;
}
napi_get_boolean(env, isSuccess, &result);
AlibabaCloud::OSS::ShutdownSdk();
return result;
}
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{ "WDOssClient_InitSdk", nullptr, WDOssClient_InitSdk, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "WDOssClient_UnInitSdk", nullptr, WDOssClient_UnInitSdk, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "WDOssClient_IsSdkInitialized", nullptr, WDOssClient_IsSdkInitialized, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "WDOssClient_UploadFile", nullptr, WDOssClient_UploadFile, nullptr, nullptr, nullptr, napi_default, nullptr }
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END
static napi_module demoModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "wdossclient",
.nm_priv = ((void*)0),
.reserved = { 0 },
};
extern "C" __attribute__((constructor)) void RegisterWdossclientModule(void)
{
napi_module_register(&demoModule);
}