aws s3 minio

news/2024/12/12 4:28:30/

#include “AwsClient.h”

#include <aws/core/Aws.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/core/client/DefaultRetryStrategy.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <oxf/Util/onceToken.h>

#include

#include “Logger.h”
#include “common/Config.h”

namespace iCG {
AwsClient::AwsClient() {
static oxf::onceToken token(= { Aws::InitAPI(_options); },
= { Aws::ShutdownAPI(_options); });
}

AwsClient::~AwsClient() {}

void AwsClient::Init(std::string &addr, const std::string &name,
const std::string &pwd) {
_addr = addr;
// _options.loggingOptions.logLevel =
// Aws::Utils::Logging::LogLevel::Debug;
Aws::Client::ClientConfiguration cfg;

cfg.endpointOverride = addr.c_str();
cfg.scheme = Aws::Http::Scheme::HTTP;
cfg.verifySSL = false;
cfg.connectTimeoutMs = Config::Instance()._minioInfo.GetConnectTimeout();
cfg.requestTimeoutMs = Config::Instance()._minioInfo.GetWriteTimeout();
cfg.httpRequestTimeoutMs = Config::Instance()._minioInfo.GetWriteTimeout();

std::shared_ptrAws::Client::RetryStrategy retry;
retry.reset(new Aws::Client::DefaultRetryStrategy(
Config::Instance()._minioInfo.GetRetryCount()));
cfg.retryStrategy = retry; // assign to client_config

Aws::Auth::AWSCredentials cred(name.c_str(), pwd.c_str());

_client = std::make_sharedAws::S3::S3Client(
cred, cfg, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Always,
false);
}

void AwsClient::pcmAddWavHeader(std::string *headStr, int channels, int bits,
int sample_rate, int len) {
head_data_t pcm2wavHEAD;

memcpy(pcm2wavHEAD.riffType, “RIFF”, strlen(“RIFF”));
memcpy(pcm2wavHEAD.wavType, “WAVE”, strlen(“WAVE”));
pcm2wavHEAD.riffSize = 36 + len;
pcm2wavHEAD.sampleRate = sample_rate;
pcm2wavHEAD.bitsPerSample = bits;
memcpy(pcm2wavHEAD.formatType, "fmt ", strlen("fmt "));
pcm2wavHEAD.formatSize = 16;
pcm2wavHEAD.numChannels = channels;
pcm2wavHEAD.blockAlign = channels * bits / 8;
pcm2wavHEAD.compressionCode = 1;
pcm2wavHEAD.bytesPerSecond = pcm2wavHEAD.sampleRate * pcm2wavHEAD.blockAlign;
memcpy(pcm2wavHEAD.dataType, “data”, strlen(“data”));
pcm2wavHEAD.dataSize = len;
(*headStr).append((char *)&pcm2wavHEAD, 44);
// fseek(fp, 0, SEEK_SET);
// fwrite(&pcm2wavHEAD, 44, 1, fp);
}

bool AwsClient::Upload(const std::string &key, const std::string &content,
std::string bucket, bool audioFlag) {
//上传到minio
bool ret = true;
// wav头
std::string uploadStr = “”;
if (audioFlag) {
pcmAddWavHeader(&uploadStr, 1, 16, 8000, content.size());
uploadStr.append(content.data(), content.size());
} else {
uploadStr = content;
}

//上传到minio
Aws::S3::Model::PutObjectRequest putObjectRequest;
putObjectRequest.WithBucket(bucket.c_str()).WithKey(key.c_str());
auto contents_stream =
Aws::MakeSharedAws::StringStream(“PutObjectInputStream”);
contents_stream->write((char *)uploadStr.data(), uploadStr.size());
putObjectRequest.SetBody(contents_stream);

//同步发送
#if 1
auto putObjectResult = _client->PutObject(putObjectRequest);
if (not putObjectResult.IsSuccess()) {
S_LOG_ERROR(_addr << “, Upload " << key << " failed!!!”
<< putObjectResult.GetError().GetMessage().c_str()
<< (int)putObjectResult.GetError().GetErrorType());
ret = _health = false;
} else {
S_LOG_INFO(_addr << ", Upload " << key << " success!!! " << _count);
}
_count–;
return ret;
#endif
//异步发送
#if 0
std::shared_ptrAws::Client::AsyncCallerContext context =
Aws::MakeSharedAws::Client::AsyncCallerContext(
“PutObjectAllocationTag”);

context->SetUUID(key.c_str());strongClient->_client->PutObjectAsync(putObjectRequest,[](const Aws::S3::S3Client *s3Client,const Aws::S3::Model::PutObjectRequest &request,const Aws::S3::Model::PutObjectOutcome &outcome,const std::shared_ptr<const Aws::Client::AsyncCallerContext>&context) {if (outcome.IsSuccess()) {S_LOG_INFO("Success: PutObjectAsyncFinished: Finished uploading '"<< context->GetUUID());} else {S_LOG_ERROR("Error: PutObjectAsyncFinished: "<< outcome.GetError().GetMessage());}},context);

#endif
}

bool AwsClient::Download(const std::string &key, std::string &fileStr) {
bool ret = true;
Aws::S3::Model::GetObjectRequest object_request;
object_request.WithBucket(“”).WithKey(key.c_str());
auto get_object_outcome = _client->GetObject(object_request);

if (get_object_outcome.IsSuccess()) {
std::ostringstream tmp;
tmp << get_object_outcome.GetResult().GetBody().rdbuf();
fileStr = tmp.str();
S_LOG_INFO(_addr << ", Download " << key << " success!!! " << _count);
} else {
S_LOG_ERROR(_addr << “, Download " << key << " failed!!!”
<< get_object_outcome.GetError().GetMessage().c_str()
<< (int)get_object_outcome.GetError().GetErrorType());
ret = _downloadHealth = false;
}
_count–;
return ret;
}

bool AwsClient::checkUnHealthClient() {
if (!_health || !_downloadHealth) {
auto putObjectResult = _client->ListBuckets();
if (not putObjectResult.IsSuccess()) {
return false;
}
_health = _downloadHealth = true;
return _health;
} else {
return true;
}
}
} // namespace iCG


http://www.ppmy.cn/news/362155.html

相关文章

UFS详细介绍---终章

UFS详细介绍—终章 UNIVERSAL FLASH STORAGE (UFS)&#xff0c;通用闪存存储器。目前最新的标准是UFS4.0&#xff1b;UFS的出现是因为替代eMMC产品的&#xff0c;但是因为价格等&#xff0c;目前没办法做到完全替代。 2020年&#xff0c;JEDEC发布了UFS3.1&#xff1b;2022年…

mq常见问题:消息丢失、消息重复消费、消息保证顺序

mq常见问题&#xff1a;消息丢失、消息重复消费、消息保证顺序 消息丢失问题 拿rabbitmq举例来说&#xff0c;出现消息丢失的场景如下图 从图中可以看到一共有以下三种可能出现消息丢失的情况&#xff1a; 1> 生产者丢消息 生产者在将数据发送到MQ的时候&#xff0c;可能…

python疑难进阶手册(4)-函数参数、正则表达式(2)

目录 函数参数位置参数关键字参数可变(数量)关键字参数函数作为函数的参数函数工厂匿名函数正则表达式函数参数 位置参数 先来一段位置参数,即:传入参数的顺序严格遵守函数定义时的参数先后顺序 #!/usr/bin/env python3 # -*- coding: utf-8 -*-def compute(x,a=1,b=

接口文档设计注意事项

接口名称清晰 一般接口url要求能看得出接口的作用。比如说&#xff0c;查询用户信息&#xff08;queryUserInfo&#xff09;&#xff0c;就是一个不错的接口名称。 接口地址完整 接口的地址&#xff0c;也叫接口的URL地址。即别人调用你的接口&#xff0c;用的是什么URL。比…

免费获取程序员好书,这个宝藏网站等你来挖!

今天给大家分享一个免费的程序员、IT、互联网资源的​免费书籍网站。​ 书籍类型涵盖了Vue、CSS、小程序等前端语言、框架&#xff1b;.NET、JAVA、Go等后端语言&#xff1b;Android、IOS、Flutter等移动语言&#xff1b;数据库、云计算大数据、网络系统运维、UI美工设计、游戏…

Windows Terminal必备终端神器

一、简介 Windows 终端是一个面向命令行工具和 shell&#xff08;如命令提示符、PowerShell 和适用于 Linux 的 Windows 子系统 (WSL)&#xff09;用户的新式终端应用程序。 它的主要功能包括多个选项卡、窗格、Unicode 和 UTF-8 字符支持、GPU 加速文本呈现引擎&#xff0c;你…

使用Oh my Posh设置Windows PowerShell主题要点

要点&#xff1a; ①按照官方文档安装Oh my Posh。 ②在PowerShell中单独设置主题一次可执行Set-PoshPrompt -Theme powerlevel10k_classic。若要每次打开PowerShell自动设置主题&#xff0c;需要在C:\Users\myname\Documents\WindowsPowerShell文件夹下新建Microsoft.PowerS…

windows终端美化 / windows terminal / windows powershell

windows 终端美化 Windows Terminal是 windows 一个终端工具&#xff0c;我们可以美化它来让自己赏心悦目。 通常电脑系统自带&#xff0c;如果没有的话可以通过 Microsoft Store&#xff08;微软商店&#xff09;&#xff0c;搜索 Windows Terminal 进行安装。 如果遇到了一…