<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>访问 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/%E8%AE%BF%E9%97%AE/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Mon, 14 Nov 2022 16:32:43 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] 通过第三方工具/JAVA程序访问S3存储</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e9%80%9a%e8%bf%87%e7%ac%ac%e4%b8%89%e6%96%b9%e5%b7%a5%e5%85%b7-java%e7%a8%8b%e5%ba%8f%e8%ae%bf%e9%97%aes3%e5%ad%98%e5%82%a8/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e9%80%9a%e8%bf%87%e7%ac%ac%e4%b8%89%e6%96%b9%e5%b7%a5%e5%85%b7-java%e7%a8%8b%e5%ba%8f%e8%ae%bf%e9%97%aes3%e5%ad%98%e5%82%a8/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Mon, 14 Nov 2022 16:32:43 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Amazon S3]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[s3-command-line]]></category>
		<category><![CDATA[对象存储]]></category>
		<category><![CDATA[访问]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=13652</guid>

					<description><![CDATA[<p>S3是亚马逊2006年推出的简单存储服务（Simple Storage Service），理论上是一个全球存储区域网络，你可以把它想像成一个超大的硬盘，可以在其中存储和检索数字资产，通过 S3 存储和检索的资产被称为对象，对象存储在存储段（bucket）中。<br />
很多公司都推出了自己的对象存储服务，例如阿里云对象存储服务OSS，可以使用S3 API进行访问。<br />
<span id="more-13652"></span><br />
话不多说，我们看看如何读写S3。<br />
<span style="color:#ff0000;">➤</span> 通过第三方工具<br />
这种工具很多，<a href="https://github.com/lpflpf/s3-command-line" rel="noopener" target="_blank">s3-command-line</a>&#160;就是一个。这是一个GoLang开发的命令行工具，编译出来只有13MB。它支持文件的上传、下载、删除、列表，已经满足了大多数使用场景需求。<br />
checkout它的源码，在安装有Go语言环境的服务器上执行 go build&#160;即可编译出可执行程序 s3-command-line，这个只需要做一次，最终运行 s3-command-line&#160;软件的服务器上并不需要安装GoLang环境。<br />
在使用 s3-command-line&#160;之前，我们需要创建一个配置文件 <span style="color:#0000ff;">config.json</span>，填入我们访问S3所需的密钥等信息：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="json language-json hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">{
&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;id&#34;</span>:&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;...&#34;</span>,
&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;secret&#34;</span>:&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;...&#34;</span>,
&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;endPoint&#34;</span>:&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;http://...&#34;</span>,
&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;region&#34;</span>:&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;...&#34;</span></code></pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e9%80%9a%e8%bf%87%e7%ac%ac%e4%b8%89%e6%96%b9%e5%b7%a5%e5%85%b7-java%e7%a8%8b%e5%ba%8f%e8%ae%bf%e9%97%aes3%e5%ad%98%e5%82%a8/" class="read-more">Read More </a></section>]]></description>
										<content:encoded><![CDATA[<p>S3是亚马逊2006年推出的简单存储服务（Simple Storage Service），理论上是一个全球存储区域网络，你可以把它想像成一个超大的硬盘，可以在其中存储和检索数字资产，通过 S3 存储和检索的资产被称为对象，对象存储在存储段（bucket）中。<br />
很多公司都推出了自己的对象存储服务，例如阿里云对象存储服务OSS，可以使用S3 API进行访问。<br />
<span id="more-13652"></span><br />
话不多说，我们看看如何读写S3。<br />
<span style="color:#ff0000;">➤</span> 通过第三方工具<br />
这种工具很多，<a href="https://github.com/lpflpf/s3-command-line" rel="noopener" target="_blank">s3-command-line</a>&nbsp;就是一个。这是一个GoLang开发的命令行工具，编译出来只有13MB。它支持文件的上传、下载、删除、列表，已经满足了大多数使用场景需求。<br />
checkout它的源码，在安装有Go语言环境的服务器上执行 go build&nbsp;即可编译出可执行程序 s3-command-line，这个只需要做一次，最终运行 s3-command-line&nbsp;软件的服务器上并不需要安装GoLang环境。<br />
在使用 s3-command-line&nbsp;之前，我们需要创建一个配置文件 <span style="color:#0000ff;">config.json</span>，填入我们访问S3所需的密钥等信息：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="json language-json hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">{
&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;id&quot;</span>:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;...&quot;</span>,
&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;secret&quot;</span>:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;...&quot;</span>,
&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;endPoint&quot;</span>:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;http://...&quot;</span>,
&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;region&quot;</span>:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;...&quot;</span>
}
</code></pre>
</section>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
这些信息你可以在你使用的S3平台上找到。<br />
假设我们要读写的bucket名为 <span style="color:#0000ff;">my-bucket</span>，那么下面的命令会告诉你怎么使用 s3-command-line：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">列出指定&nbsp;bucket&nbsp;里的文件：
./s3-command-line&nbsp;ls&nbsp;-c&nbsp;config.json&nbsp;my-bucket&nbsp;/

从指定的&nbsp;bucket&nbsp;删除文件&nbsp;1.txt：
./s3-command-line&nbsp;rm&nbsp;-c&nbsp;config.json&nbsp;my-bucket&nbsp;1.txt

上传本地文件&nbsp;1.txt&nbsp;到指定的&nbsp;bucket：
./s3-command-line&nbsp;put&nbsp;-c&nbsp;config.json&nbsp;my-bucket&nbsp;1.txt

从指定的&nbsp;bucket&nbsp;下载文件&nbsp;1.txt&nbsp;到本地：
./s3-command-line&nbsp;get&nbsp;-c&nbsp;config.json&nbsp;my-bucket&nbsp;1.txt
</code></pre>
</section>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
总结：简单好用，编译成可执行程序后没有对其他library的依赖，令人舒适。</p>
<p><span style="color: rgb(255, 0, 0);">➤</span>&nbsp;自己写JAVA程序<br />
JDK版本：1.8.0_221，Maven：3.6.3<br />
首先你需要在 pom.xml&nbsp;中添加相关依赖（参考<a href="https://github.com/aws/aws-sdk-java-v2/#using-the-sdk" rel="noopener" target="_blank">这里</a>）：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="xml language-xml hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span>
&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>software.amazon.awssdk<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>
&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>aws-sdk-java<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>
&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>2.18.16<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>
<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span></code></pre>
</section>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
然后就可以开始写代码了（测试可用）：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">package</span>&nbsp;com.codelast;


<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;lombok.extern.slf4j.Slf4j;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;org.apache.commons.lang.StringUtils;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;org.kohsuke.args4j.CmdLineException;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;org.kohsuke.args4j.CmdLineParser;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;org.kohsuke.args4j.Option;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.core.sync.RequestBody;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.regions.Region;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.services.s3.S3Client;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.services.s3.model.PutObjectRequest;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.services.s3.model.PutObjectResponse;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.services.s3.model.S3Exception;

<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;java.io.File;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;java.io.FileInputStream;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;java.io.IOException;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;java.net.URI;

<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">/**
&nbsp;*&nbsp;S3存储操作类。
&nbsp;*&nbsp;参考：https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javav2/example_code/s3/src/main/java/com/example/s3/PutObject.java
&nbsp;*
&nbsp;*&nbsp;<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">@author</span>&nbsp;DarranZhang@codelast.com
&nbsp;*&nbsp;<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">@version</span>&nbsp;2022-11-14
&nbsp;*/
</span>
<span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">@Slf</span>4j
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">class</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">S3Operator</span>&nbsp;</span>{
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">final</span>&nbsp;String&nbsp;ACCESS_KEY&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;your_key&quot;</span>;
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">final</span>&nbsp;String&nbsp;SECRET_KEY&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;your_secret&quot;</span>;
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">final</span>&nbsp;String&nbsp;END_POINT&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;http://your_end_point_url&quot;</span>;
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">final</span>&nbsp;String&nbsp;REGION&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;your_region&quot;</span>;
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">final</span>&nbsp;String&nbsp;BUCKET_NAME&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;your_bucket_name&quot;</span>;

&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">private</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">class</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">Options</span>&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">@Option</span>(name&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;--localFile&quot;</span>,&nbsp;usage&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;the&nbsp;path&nbsp;of&nbsp;the&nbsp;local&nbsp;file&nbsp;to&nbsp;upload&nbsp;to&nbsp;S3&quot;</span>,&nbsp;required&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">true</span>)
&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;localFile;
&nbsp;&nbsp;}

&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">main</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(String[]&nbsp;args)</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">throws</span>&nbsp;Exception&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;Options&nbsp;opts&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;Options();
&nbsp;&nbsp;&nbsp;&nbsp;CmdLineParser&nbsp;parser&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;CmdLineParser(opts);
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">try</span>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parser.parseArgument(args);
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">catch</span>&nbsp;(CmdLineException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.error(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;command&nbsp;parse&nbsp;fail&quot;</span>,&nbsp;e);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.exit(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">1</span>);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;S3Operator&nbsp;operator&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;S3Operator();
&nbsp;&nbsp;&nbsp;&nbsp;operator.upload(opts.localFile);
&nbsp;&nbsp;}

&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">private</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">upload</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(String&nbsp;localFile)</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">throws</span>&nbsp;Exception&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;AwsBasicCredentials&nbsp;credentials&nbsp;=&nbsp;AwsBasicCredentials.create(ACCESS_KEY,&nbsp;SECRET_KEY);
&nbsp;&nbsp;&nbsp;&nbsp;Region&nbsp;region&nbsp;=&nbsp;Region.of(REGION);
&nbsp;&nbsp;&nbsp;&nbsp;S3Client&nbsp;s3Client&nbsp;=&nbsp;S3Client.builder()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.endpointOverride(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;URI(END_POINT))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.region(region)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.credentialsProvider(StaticCredentialsProvider.create(credentials))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.build();

&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;s3FileName&nbsp;=&nbsp;StringUtils.substringAfterLast(localFile,&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;/&quot;</span>);&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;上传到S3之后的文件名，不一定要和本地文件名一致，但为了直观，这里保持一致</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&nbsp;(StringUtils.isEmpty(s3FileName))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;msg&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;local&nbsp;file&nbsp;name&nbsp;is&nbsp;empty&quot;</span>;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.error(msg);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">throw</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;RuntimeException(msg);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;每次上传前，不用先删除再上传，因为最新上传的同名文件会覆盖之前的文件</span>
&nbsp;&nbsp;&nbsp;&nbsp;putObject(s3Client,&nbsp;BUCKET_NAME,&nbsp;s3FileName,&nbsp;localFile);
&nbsp;&nbsp;&nbsp;&nbsp;s3Client.close();
&nbsp;&nbsp;}

&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">putObject</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(S3Client&nbsp;s3Client,&nbsp;String&nbsp;bucketName,&nbsp;String&nbsp;objectKey,&nbsp;String&nbsp;objectPath)</span>&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">try</span>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PutObjectRequest&nbsp;request&nbsp;=&nbsp;PutObjectRequest.builder().bucket(bucketName).key(objectKey).build();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PutObjectResponse&nbsp;response&nbsp;=&nbsp;s3Client.putObject(request,&nbsp;RequestBody.fromBytes(getObjectFile(objectPath)));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.info(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;successfully&nbsp;upload&nbsp;file&nbsp;[{}]&nbsp;to&nbsp;S3,&nbsp;entity&nbsp;tag:&nbsp;[{}]&quot;</span>,&nbsp;objectPath,&nbsp;response.eTag());
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">catch</span>&nbsp;(S3Exception&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.error(String.format(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;failed&nbsp;to&nbsp;upload&nbsp;file&nbsp;[%s]&nbsp;to&nbsp;S3&quot;</span>,&nbsp;objectPath),&nbsp;e);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}

&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">private</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">byte</span>[]&nbsp;getObjectFile(String&nbsp;filePath)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;FileInputStream&nbsp;fileInputStream&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>;
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">byte</span>[]&nbsp;bytesArray&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>;

&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">try</span>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File&nbsp;file&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;File(filePath);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bytesArray&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">byte</span>[(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">int</span>)&nbsp;file.length()];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fileInputStream&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;FileInputStream(file);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fileInputStream.read(bytesArray);
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">catch</span>&nbsp;(IOException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">finally</span>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&nbsp;(fileInputStream&nbsp;!=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">try</span>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fileInputStream.close();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">catch</span>&nbsp;(IOException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">return</span>&nbsp;bytesArray;
&nbsp;&nbsp;}

&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">private</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">deleteObject</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(S3Client&nbsp;s3,&nbsp;String&nbsp;bucket,&nbsp;String&nbsp;s3FileName)</span>&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;DeleteObjectRequest&nbsp;request&nbsp;=&nbsp;DeleteObjectRequest.builder().bucket(bucket).key(s3FileName).build();
&nbsp;&nbsp;&nbsp;&nbsp;s3.deleteObject(request);
&nbsp;&nbsp;}
}
</code></pre>
</section>
<p> 这样我们就实现了用JAVA访问S3，不过，如果你仔细看会发现，当引入S3的依赖后，你的项目编译出的 fat jar&nbsp;体积剧增了400MB左右！这也太恶心了...<br />
当然，上面引入的 whole-SDK 依赖，是我为了省事引入了所有S3相关的模块，或许你有些用不到的模块，可以不用引入 whole-SDK，而是分别引入单独的模块，但我有空去想这些玩意的话，还不如使用 s3-command-line&nbsp;简单呢，不是吗？毕竟时间就是生命，别在这些无聊的事上浪费生命。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤</span>&nbsp;总结<br />
如果你操作S3时只使用上传、下载、删除、列表功能，那么建议不要自己写代码，使用 s3-command-line&nbsp;更香。</p>
<p>
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：<br />
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="color: rgb(77, 77, 77); font-size: 13px; width: 200px; height: 200px;" /><br />
	以及我的微信视频号：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="" src="https://www.codelast.com/wechat_shipinhao_qr_code.jpg" style="text-align: center; width: 200px; height: 199px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e9%80%9a%e8%bf%87%e7%ac%ac%e4%b8%89%e6%96%b9%e5%b7%a5%e5%85%b7-java%e7%a8%8b%e5%ba%8f%e8%ae%bf%e9%97%aes3%e5%ad%98%e5%82%a8/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创]使用C++（通过Thrift）访问/操作/读写Hbase</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e4%bd%bf%e7%94%a8c%ef%bc%88%e9%80%9a%e8%bf%87thrift%ef%bc%89%e8%ae%bf%e9%97%aehbase/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e4%bd%bf%e7%94%a8c%ef%bc%88%e9%80%9a%e8%bf%87thrift%ef%bc%89%e8%ae%bf%e9%97%aehbase/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Thu, 01 Sep 2011 05:40:43 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Boost]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[client]]></category>
		<category><![CDATA[connect]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[Hbase]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[libevent]]></category>
		<category><![CDATA[operate]]></category>
		<category><![CDATA[read]]></category>
		<category><![CDATA[sample]]></category>
		<category><![CDATA[Thrift]]></category>
		<category><![CDATA[write]]></category>
		<category><![CDATA[例子]]></category>
		<category><![CDATA[操作Hbase]]></category>
		<category><![CDATA[教程]]></category>
		<category><![CDATA[方法]]></category>
		<category><![CDATA[示例]]></category>
		<category><![CDATA[访问]]></category>
		<category><![CDATA[访问Hbase]]></category>
		<category><![CDATA[读写]]></category>
		<category><![CDATA[读写HBase]]></category>
		<category><![CDATA[读取]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=3303</guid>

					<description><![CDATA[<p>
	<span style="font-family: 宋体; font-size: 14px;">无奈，网上关于C++访问Hbase的文章实在太少，所以只好自己折腾一下，然后写出来了。</span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">要使用C++访问Hbase，可以走的途径少之又少，据说当前最好的方法就是通过Thrift来实现：</span></span></span><span style="font-size: 14px;"><span style="font-family: 宋体;"><a href="http://thrift.apache.org/" target="_blank" rel="noopener noreferrer"><span style="color: #000000;">http://thrift.apache.org/</span></a></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">所以本文分成几部分：（1）安装Thrift；（2）用Thrift 生成访问Hbase所需的C++文件；（3）在程序中通过Thrift来访问Hbase。</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">另外，本文只包含读写Hbase数据的例子，不包含配置Hbase的方法，如需这些内容，请自行搜索。</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">首先声明一下，本文基于以下环境：<br />
	<span id="more-3303"></span><!--more--><!--more--><!--more--><!--more--><!--more--><!--more--></span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">操作系统：RHEL 5.3，64位</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">Thrift 版本：0.7.0</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">要访问的 Hbase 版本：0.20.6</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">我使用0.90.4的 Hbase 安装包来生成C++所需的Hbase.h等文件（用新版的应该能兼容旧版的）</span></span></span></p>
<p>
	<span style="color: rgb(255, 255, 255); font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; text-align: left; background-color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">下面开始，一步步来。</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">（1）安装Thrift</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">不是一件很轻松的事。如果你的系统比较干净，可能很顺利地就搞定了，如果有依赖库缺失问题或者库冲突问题，那么就只能根据具体情况，一个个问题去fix了，谁知道会有多少麻烦。</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">我运气比较好，在一个干净的系统上，很快就完成了。</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">Thrift 至少会依赖于两个系统中一般不会带的库：libevent，boost。</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">libevent 到这里下载：</span></span></span><span style="font-size: 14px;"><span style="font-family: 宋体;"><a href="http://monkey.org/~provos/libevent/" target="_blank" rel="noopener noreferrer"><span style="color: #000000;">http://monkey.org/~provos/libevent/</span></a></span></span><span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">&#160; 我使用的版本是：2.0.12-stable</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">boost 到这里下载：</span></span></span><span style="font-size: 14px;"><span style="font-family: 宋体;"><a href="http://www.boost.org/" target="_blank" rel="noopener noreferrer"><span style="color: #000000;">http://www.boost.org/</span></a></span></span><span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">&#160; 我使用的版本是：1.47.0</span></span></span></p>
<p>
	<span style="color: rgb(255, 255, 255); font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; text-align: left; background-color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">安装libevent：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
./configure --prefix=/usr/local/libevent
make
make install</pre>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">安装boost（boost不像一般的Linux源码安装包一样，它的安装不是configure，make，make install，有点怪）：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
./bootstrap.sh</pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b%e4%bd%bf%e7%94%a8c%ef%bc%88%e9%80%9a%e8%bf%87thrift%ef%bc%89%e8%ae%bf%e9%97%aehbase/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
	<span style="font-family: 宋体; font-size: 14px;">无奈，网上关于C++访问Hbase的文章实在太少，所以只好自己折腾一下，然后写出来了。</span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">要使用C++访问Hbase，可以走的途径少之又少，据说当前最好的方法就是通过Thrift来实现：</span></span></span><span style="font-size: 14px;"><span style="font-family: 宋体;"><a href="http://thrift.apache.org/" target="_blank" rel="noopener noreferrer"><span style="color: #000000;">http://thrift.apache.org/</span></a></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">所以本文分成几部分：（1）安装Thrift；（2）用Thrift 生成访问Hbase所需的C++文件；（3）在程序中通过Thrift来访问Hbase。</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">另外，本文只包含读写Hbase数据的例子，不包含配置Hbase的方法，如需这些内容，请自行搜索。</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">首先声明一下，本文基于以下环境：<br />
	<span id="more-3303"></span><!--more--><!--more--><!--more--><!--more--><!--more--><!--more--></span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">操作系统：RHEL 5.3，64位</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">Thrift 版本：0.7.0</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">要访问的 Hbase 版本：0.20.6</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">我使用0.90.4的 Hbase 安装包来生成C++所需的Hbase.h等文件（用新版的应该能兼容旧版的）</span></span></span></p>
<p>
	<span style="color: rgb(255, 255, 255); font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; text-align: left; background-color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">下面开始，一步步来。</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">（1）安装Thrift</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">不是一件很轻松的事。如果你的系统比较干净，可能很顺利地就搞定了，如果有依赖库缺失问题或者库冲突问题，那么就只能根据具体情况，一个个问题去fix了，谁知道会有多少麻烦。</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">我运气比较好，在一个干净的系统上，很快就完成了。</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">Thrift 至少会依赖于两个系统中一般不会带的库：libevent，boost。</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">libevent 到这里下载：</span></span></span><span style="font-size: 14px;"><span style="font-family: 宋体;"><a href="http://monkey.org/~provos/libevent/" target="_blank" rel="noopener noreferrer"><span style="color: #000000;">http://monkey.org/~provos/libevent/</span></a></span></span><span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">&nbsp; 我使用的版本是：2.0.12-stable</span></span></span></p>
<p>
	<span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">boost 到这里下载：</span></span></span><span style="font-size: 14px;"><span style="font-family: 宋体;"><a href="http://www.boost.org/" target="_blank" rel="noopener noreferrer"><span style="color: #000000;">http://www.boost.org/</span></a></span></span><span style="color: #000000;"><span style="font-size: 14px;"><span style="font-family: 宋体;">&nbsp; 我使用的版本是：1.47.0</span></span></span></p>
<p>
	<span style="color: rgb(255, 255, 255); font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; text-align: left; background-color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">安装libevent：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
./configure --prefix=/usr/local/libevent
make
make install</pre>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">安装boost（boost不像一般的Linux源码安装包一样，它的安装不是configure，make，make install，有点怪）：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
./bootstrap.sh --prefix=/usr/local/boost</pre>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">不出错的话接着执行以下命令开始编译（也可以通过编辑project-config.jam文件调整编译参数）：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
./b2
./b2 install</pre>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">安装Thrift：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
chmod +x configure
./configure --with-boost=/usr/local --prefix=/usr/local/thrift
make
make install</pre>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">至此，安装Thrift 的工作就完成了。</span></span></p>
<p>
	<span style="color: rgb(255, 255, 255); font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; text-align: left; background-color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">（2）用Thrift 生成访问Hbase所需的C++文件</span></span></p>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">访问Hbase需要在你的程序中使用若干.h，.cpp文件，这些文件是用 Thrift 生成的。</span></span></p>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">解压Hbase源码安装包：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
tar zxf hbase-0.90.4.tar.gz
cd hbase-0.90.4</pre>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">在解压出来的文件中， 你可以找到一个名为 Hbase.thrift 的文件，这个文件定义了如何通过 Thrift 接口来访问Hbase。用这个Thrift文件，可以生成访问Hbase所需的C++文件：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
/usr/local/thrift/bin/thrift --gen cpp ./src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift</pre>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">会发现生成了gen-cpp目录：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
ls gen-cpp/</pre>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">输出：</span></span></p>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;"><span style="color: #0000ff;">Hbase_constants.cpp &nbsp;Hbase_constants.h &nbsp;Hbase.cpp &nbsp;Hbase.h &nbsp;Hbase_server.skeleton.cpp &nbsp;Hbase_types.cpp &nbsp;Hbase_types.h</span></span></span></p>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">除了Hbase_server.skeleton.cpp之外，其余文件都是在我们的程序里要用到的，将它们拷贝到我们的工程目录下。</span></span></p>
<p>
	<span style="color: rgb(255, 255, 255); font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; text-align: left; background-color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">（3）在程序中使用Thrift来访问Hbase</span></span></p>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">要能通过 Thrift 访问Hbase，你必须首先要打开HBase的 Thrift 服务，请参考其他文档确保这一点是可用的。</span></span></p>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">下一步，我们在程序中如何读取Hbase的数据？</span></span></p>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">我们先看看hbase源码安装包中自带的例子：在解压出来的安装包中的&nbsp;examples/thrift/ 目录下的&nbsp;DemoClient.cpp 文件，有如下代码：</span></span></p>
<div>
<pre class="brush:cpp;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
  boost::shared_ptr&lt;TTransport&gt; socket(new TSocket(&quot;localhost&quot;, 9090));
  boost::shared_ptr&lt;TTransport&gt; transport(new TBufferedTransport(socket));
  boost::shared_ptr&lt;TProtocol&gt; protocol(new TBinaryProtocol(transport));
  HbaseClient client(protocol);
  try {
    transport-&gt;open();

    // do something

    transport-&gt;close();
  } catch (TException &amp;tx) {
    printf(&quot;ERROR: %s\n&quot;, tx.what());
  }</pre>
</div>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">我们就仿照这个例子来做。从DemoClient.cpp可见，我们要先创建三个指针socket，transport和protocol，后两个分别依赖于前两个，最后再创建一个client对象，我们操作Hbase就是使用这个client对象。在操作Hbase前，需要先打开到Hbase Thrift service的连接，即 transport-&gt;open()，在操作完 Hbase之后，需要关闭连接，即 transport-&gt;close()，这下就比较清楚了：我们可以写一个自己的类CHbaseOperate，它应该有一个connect函数和一个disconnect函数，分别用于打开、关闭连接，还应该有读写Hbase的基本功能。读写Hbase的方法，请参考Hbase.h中的函数，例子还是看DemoClient.cpp。</span></span><br />
	<span style="color: rgb(255, 255, 255); font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; text-align: left; background-color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">下面上代码：<br />
	<span style="background-color: #00ff00;">HbaseOperate.h：</span></span></span></p>
<pre class="brush:cpp;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
#ifndef __HBASE_OPERATE_H
#define __HBASE_OPERATE_H

#include &lt;string&gt;
#include &lt;protocol/TBinaryProtocol.h&gt;
#include &lt;transport/TSocket.h&gt;
#include &lt;transport/TTransportUtils.h&gt;
#include &quot;Hbase.h&quot;

/**
 * Class to operate Hbase.
 *
 * @author Darran Zhang (codelast.com)
 * @version 11-08-24
 * @declaration These codes are only for non-commercial use, and are distributed on an &quot;AS IS&quot; basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
 * You must not remove this declaration at any time.
 */

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::hadoop::hbase::thrift;

typedef struct hbaseRet {
  std::string rowValue;
  time_t ts;

  hbaseRet() {
    ts = 0;
  }

} hbaseRet;

class CHbaseOperate
{
public:
	CHbaseOperate();
	virtual ~CHbaseOperate();

private:
  boost::shared_ptr&lt;TTransport&gt; socket;
  boost::shared_ptr&lt;TTransport&gt; transport;
  boost::shared_ptr&lt;TProtocol&gt; protocol;

  HbaseClient *client;

  std::string  hbaseServiceHost;
  int     hbaseServicePort;
  bool    isConnected;

public:
  bool  connect();

  bool  connect(std::string host, int port);

  bool  disconnect();

  bool  putRow(const std::string &amp;tableName,
              const std::string &amp;rowKey,
              const std::string &amp;column,
              const std::string &amp;rowValue);

  bool  getRow(hbaseRet &amp;result,
              const std::string &amp;tableName,
              const std::string &amp;rowKey,
              const std::string &amp;columnName);
};

#endif</pre>
<p>
	<span style="color: rgb(255, 255, 255); font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; text-align: left; background-color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;"><span style="background-color: #00ff00;">HbaseOperate.cpp：</span></span></span></p>
<pre class="brush:cpp;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
#include &quot;HbaseOperate.h&quot;
#include &quot;log4cxx/log4cxx.h&quot;
#include &quot;log4cxx/propertyconfigurator.h&quot;

static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger(&quot;HbaseOperate.cpp&quot;));

/**
 * Class to operate Hbase.
 *
 * @author Darran Zhang (codelast.com)
 * @version 11-08-24
 * @declaration These codes are only for non-commercial use, and are distributed on an &quot;AS IS&quot; basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
 * You must not remove this declaration at any time.
 */

using namespace std;

CHbaseOperate::CHbaseOperate() :
socket((TSocket*)NULL), transport((TBufferedTransport*)NULL), protocol((TBinaryProtocol*)NULL), client(NULL), hbaseServicePort(9090), isConnected(false)
{
}

CHbaseOperate::~CHbaseOperate()
{
  if (isConnected) {
    disconnect();
  }
  if (NULL != client) {
    delete client;
    client = NULL;
  }
}

/**
 * Connect Hbase.
 *
 */
bool CHbaseOperate::connect()
{
  if (isConnected) {
    LOG4CXX_INFO(logger, &quot;Already connected, don&#39;t need to connect it again&quot;);
    return true;
  }

  try {
    socket.reset(new TSocket(hbaseServiceHost, hbaseServicePort));
    transport.reset(new TBufferedTransport(socket));
    protocol.reset(new TBinaryProtocol(transport));

    client = new HbaseClient(protocol);

    transport-&gt;open();
  } catch (const TException &amp;tx) {
    LOG4CXX_ERROR(logger, &quot;Connect Hbase error : &quot; &lt;&lt; tx.what());
    return false;
  }

  isConnected = true;
  return isConnected;
}

/**
 * Connect Hbase.
 *
 */
bool CHbaseOperate::connect(std::string host, int port)
{
  hbaseServiceHost = host;
  hbaseServicePort = port;

  return connect();
}

/**
 * Disconnect from Hbase.
 *
 */
bool CHbaseOperate::disconnect()
{
  if (!isConnected) {
    LOG4CXX_ERROR(logger, &quot;Haven&#39;t connected to Hbase yet, can&#39;t disconnect from it&quot;);
    return false;
  }

  if (NULL != transport) {
    try {
      transport-&gt;close();
    } catch (const TException &amp;tx) {
      LOG4CXX_ERROR(logger, &quot;Disconnect Hbase error : &quot; &lt;&lt; tx.what());
      return false;
    }
  } else {
    return false;
  }

  isConnected = false;
  return true;
}

/**
 * Put a row to Hbase.
 *
 * @param tableName   [IN] The table name.
 * @param rowKey      [IN] The row key.
 * @param column      [IN] The &quot;column family : qualifier&quot;.
 * @param rowValue    [IN] The row value.
 * @return True for successfully put the row, false otherwise.
 */
bool CHbaseOperate::putRow(const string &amp;tableName, const string &amp;rowKey, const string &amp;column, const string &amp;rowValue)
{
  if (!isConnected) {
    LOG4CXX_ERROR(logger, &quot;Haven&#39;t connected to Hbase yet, can&#39;t put row&quot;);
    return false;
  }

  try {
    std::vector&lt;Mutation&gt; mutations;
    mutations.push_back(Mutation());
    mutations.back().column = column;
    mutations.back().value = rowValue;
    client-&gt;mutateRow(tableName, rowKey, mutations);

  } catch (const TException &amp;tx) {
    LOG4CXX_ERROR(logger, &quot;Operate Hbase error : &quot; &lt;&lt; tx.what());
    return false;
  }

  return true;
}

/**
 * Get a Hbase row.
 *
 * @param result      [OUT] The object which contains the returned data.
 * @param tableName   [IN] The Hbase table name, e.g. &quot;MyTable&quot;.
 * @param rowKey      [IN] The Hbase row key, e.g. &quot;kdr23790&quot;.
 * @param columnName  [IN] The &quot;column family : qualifier&quot;.
 * @return True for successfully get the row, false otherwise.
 */
bool CHbaseOperate::getRow(hbaseRet &amp;result, const std::string &amp;tableName, const std::string &amp;rowKey, const std::string &amp;columnName)
{
  if (!isConnected) {
    LOG4CXX_ERROR(logger, &quot;Haven&#39;t connected to Hbase yet, can&#39;t read data from it&quot;);
    return false;
  }

  std::vector&lt;std::string&gt; columnNames;
  columnNames.push_back(columnName);

  std::vector&lt;TRowResult&gt; rowResult;
  try {
    client-&gt;getRowWithColumns(rowResult, tableName, rowKey, columnNames);
  } catch (const TException &amp;tx) {
    LOG4CXX_ERROR(logger, &quot;Operate Hbase error : &quot; &lt;&lt; tx.what());
    return false;
  }

  if (0 == rowResult.size()) {
    LOG4CXX_WARN(logger, &quot;Got no record with the key : [&quot; &lt;&lt; rowKey &lt;&lt; &quot;]&quot;);
    return false;
  }

  std::map&lt;std::string, TCell&gt;::const_iterator it = rowResult[rowResult.size() -1].columns.begin();
  result.rowValue = it-&gt;second.value;
  result.ts = it-&gt;second.timestamp;

  return true;
}</pre>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">注意我在程序中使用了<a href="http://logging.apache.org/log4cxx/" target="_blank" rel="noopener noreferrer">Apache log4cxx</a>这个记录日志的库来打印/保存程序运行日志，使用方法可参考<span style="color: #ff0000;">【</span><a href="http://www.codelast.com/?p=3211" target="_blank" rel="noopener noreferrer">此链接</a><span style="color: #ff0000;">】</span>。如果你不想用，可以自己改为std::cout。<br />
	代码有了，使用方法为：可以在你的程序中创建一个全局对象：</span></span></p>
<pre class="brush:cpp;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
CHbaseOperate g_ho;</pre>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">在需要操作Hbase之前：</span></span></p>
<pre class="brush:cpp;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
g_ho.connect(&quot;192.168.55.66&quot;, 9090);</pre>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;">其中，&ldquo;192.168.55.66&rdquo;和9090分别是你的Hbase Thrift service的服务器地址和端口号，你需要正确地配置好，才能使用。本文开头已经说了，本文不讨论这方面的问题。<br />
	在操作完Hbase之后：</span></span></p>
<pre class="brush:cpp;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
g_ho.disconnect();</pre>
<p>
	<span style="color: rgb(255, 255, 255); font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; text-align: left; background-color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
	<span style="font-size: 14px;"><span style="font-family: 宋体;"> <span style="color: #000000;">现在再来说一下读写操作Hbase的两个函数：putRow()和getRow()。<br />
	<span style="background-color: #40e0d0;">putRow()：</span></span></span></span></p>
<pre class="brush:cpp;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
bool  putRow(const std::string &amp;tableName,
              const std::string &amp;rowKey,
              const std::string &amp;column,
              const std::string &amp;rowValue);</pre>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;"><span style="color: #000000;">这是向Hbase写入一条记录的函数，参数tableName为Hbase表名，即你要将记录写到哪个Hbase表中；参数rowKey为待写入的记录的key；参数column为待写入的记录的&ldquo;column family:qualifier&rdquo;组合，参数rowValue为待写入的记录的value。</span></span></span></p>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;"><span style="background-color: #40e0d0;">getRow()：</span></span></span></p>
<pre class="brush:cpp;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
bool  getRow(hbaseRet &amp;result,
              const std::string &amp;tableName,
              const std::string &amp;rowKey,
              const std::string &amp;columnName);</pre>
<p>
	<span style="font-size: 14px;"><span style="font-family: 宋体;"><span style="color: #000000;">这是从Hbase中读取一条记录的函数，参数tableName为Hbase表名，即你要从哪个Hbase表中读取记录；参数rowKey为你要查询的记录的key；参数columnName为你要查询的记录的&ldquo;column family:qualifier&rdquo;组合;参数result为返回的Hbase的数据，它包含记录的value和记录的时间戳：</span></span></span></p>
<pre class="brush:cpp;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
typedef struct hbaseRet {
  std::string rowValue;
  time_t ts; 

  hbaseRet() {
    ts = 0;
  }

} hbaseRet;</pre>
<p>
	<span style="color: rgb(255, 255, 255); font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; text-align: left; background-color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
	<span style="font-size: 14px;"><span style="font-family: 宋体;"> <span style="color: #000000;">至于操作的结果对不对，可以在Hbase shell中用get, scan等命令来验证，具体方法请看Hbase shell的help。另外，最好再写一些unit test来测试。<br />
	如果你要为CHbaseOperate类添加功能，可以参考Hbase.h文件中的函数定义。如你所见，CHbaseOperate类主要也是调用了里面的函数，只不过这个类可以让一些不太熟悉Hbase概念的人可以更方便地操作Hbase罢了。</span></span></span></p>
<p>	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushShell.js"></script>
<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushCpp.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']	= true;
			SyntaxHighlighter.defaults['tab-size']		= 2;
			SyntaxHighlighter.defaults['gutter']		= true;
			SyntaxHighlighter.defaults['quick-code']	= true;
			SyntaxHighlighter.defaults['collapse'] 		= false;
			SyntaxHighlighter.defaults['auto-links']	= true;
			SyntaxHighlighter.defaults['toolbar']		= true;
			SyntaxHighlighter.all();
			</script>
			<!--[/syntaxhighlighter]-->]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e4%bd%bf%e7%94%a8c%ef%bc%88%e9%80%9a%e8%bf%87thrift%ef%bc%89%e8%ae%bf%e9%97%aehbase/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
	</channel>
</rss>
