mirror of
https://github.com/JasonsGong/JasonsGong.github.io.git
synced 2024-11-24 11:49:36 +08:00
318 lines
110 KiB
HTML
318 lines
110 KiB
HTML
<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>阿里云对象存储OSS | The Blog</title><meta name="author" content="Jason"><meta name="copyright" content="Jason"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="1.官网介绍 阿里云对象存储OSS(Object Storage Service)是一款海量、安全、低成本、高可靠的云存储服务,可提供99.9999999999%(12个9)的数据持久性,99.995%的数据可用性。多种存储类型供选择,全面优化存储成本。OSS具有与平台无关的RESTful API接口,您可以在任何应用、任何时间、任何地点存储和访问任意类型的数据。您可以使用阿里云提供的AP">
|
||
<meta property="og:type" content="article">
|
||
<meta property="og:title" content="阿里云对象存储OSS">
|
||
<meta property="og:url" content="https://qingling.icu/posts/6319.html">
|
||
<meta property="og:site_name" content="The Blog">
|
||
<meta property="og:description" content="1.官网介绍 阿里云对象存储OSS(Object Storage Service)是一款海量、安全、低成本、高可靠的云存储服务,可提供99.9999999999%(12个9)的数据持久性,99.995%的数据可用性。多种存储类型供选择,全面优化存储成本。OSS具有与平台无关的RESTful API接口,您可以在任何应用、任何时间、任何地点存储和访问任意类型的数据。您可以使用阿里云提供的AP">
|
||
<meta property="og:locale" content="zh_CN">
|
||
<meta property="og:image" content="https://qingling.icu/img/9.png">
|
||
<meta property="article:published_time" content="2023-04-22T12:54:02.000Z">
|
||
<meta property="article:modified_time" content="2023-09-11T23:36:48.000Z">
|
||
<meta property="article:author" content="Jason">
|
||
<meta property="article:tag" content="云计算">
|
||
<meta name="twitter:card" content="summary">
|
||
<meta name="twitter:image" content="https://qingling.icu/img/9.png"><link rel="shortcut icon" href="/img/%E5%9B%BE%E6%A0%87.png"><link rel="canonical" href="https://qingling.icu/posts/6319.html"><link rel="preconnect" href="//fastly.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="/cdn/icon/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="/cdn/css/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="/cdn/css/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
|
||
root: '/',
|
||
algolia: undefined,
|
||
localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容:${query}","hits_stats":"共找到 ${hits} 篇文章"}},
|
||
translate: undefined,
|
||
noticeOutdate: undefined,
|
||
highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":400},
|
||
copy: {
|
||
success: '复制成功',
|
||
error: '复制错误',
|
||
noSupport: '浏览器不支持'
|
||
},
|
||
relativeDate: {
|
||
homepage: true,
|
||
post: true
|
||
},
|
||
runtime: '天',
|
||
dateSuffix: {
|
||
just: '刚刚',
|
||
min: '分钟前',
|
||
hour: '小时前',
|
||
day: '天前',
|
||
month: '个月前'
|
||
},
|
||
copyright: undefined,
|
||
lightbox: 'mediumZoom',
|
||
Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#006650","bgDark":"#006650","position":"top-center"},
|
||
source: {
|
||
justifiedGallery: {
|
||
js: 'https://fastly.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
|
||
css: 'https://fastly.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
|
||
}
|
||
},
|
||
isPhotoFigcaption: false,
|
||
islazyload: false,
|
||
isAnchor: true,
|
||
percent: {
|
||
toc: true,
|
||
rightside: false,
|
||
},
|
||
autoDarkmode: true
|
||
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
|
||
title: '阿里云对象存储OSS',
|
||
isPost: true,
|
||
isHome: false,
|
||
isHighlightShrink: false,
|
||
isToc: true,
|
||
postUpdate: '2023-09-12 07:36:48'
|
||
}</script><noscript><style type="text/css">
|
||
#nav {
|
||
opacity: 1
|
||
}
|
||
.justified-gallery img {
|
||
opacity: 1
|
||
}
|
||
|
||
#recent-posts time,
|
||
#post-meta time {
|
||
display: inline !important
|
||
}
|
||
</style></noscript><script>(win=>{
|
||
win.saveToLocal = {
|
||
set: function setWithExpiry(key, value, ttl) {
|
||
if (ttl === 0) return
|
||
const now = new Date()
|
||
const expiryDay = ttl * 86400000
|
||
const item = {
|
||
value: value,
|
||
expiry: now.getTime() + expiryDay,
|
||
}
|
||
localStorage.setItem(key, JSON.stringify(item))
|
||
},
|
||
|
||
get: function getWithExpiry(key) {
|
||
const itemStr = localStorage.getItem(key)
|
||
|
||
if (!itemStr) {
|
||
return undefined
|
||
}
|
||
const item = JSON.parse(itemStr)
|
||
const now = new Date()
|
||
|
||
if (now.getTime() > item.expiry) {
|
||
localStorage.removeItem(key)
|
||
return undefined
|
||
}
|
||
return item.value
|
||
}
|
||
}
|
||
|
||
win.getScript = url => new Promise((resolve, reject) => {
|
||
const script = document.createElement('script')
|
||
script.src = url
|
||
script.async = true
|
||
script.onerror = reject
|
||
script.onload = script.onreadystatechange = function() {
|
||
const loadState = this.readyState
|
||
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
|
||
script.onload = script.onreadystatechange = null
|
||
resolve()
|
||
}
|
||
document.head.appendChild(script)
|
||
})
|
||
|
||
win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
|
||
const link = document.createElement('link')
|
||
link.rel = 'stylesheet'
|
||
link.href = url
|
||
if (id) link.id = id
|
||
link.onerror = reject
|
||
link.onload = link.onreadystatechange = function() {
|
||
const loadState = this.readyState
|
||
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
|
||
link.onload = link.onreadystatechange = null
|
||
resolve()
|
||
}
|
||
document.head.appendChild(link)
|
||
})
|
||
|
||
win.activateDarkMode = function () {
|
||
document.documentElement.setAttribute('data-theme', 'dark')
|
||
if (document.querySelector('meta[name="theme-color"]') !== null) {
|
||
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
|
||
}
|
||
}
|
||
win.activateLightMode = function () {
|
||
document.documentElement.setAttribute('data-theme', 'light')
|
||
if (document.querySelector('meta[name="theme-color"]') !== null) {
|
||
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
|
||
}
|
||
}
|
||
const t = saveToLocal.get('theme')
|
||
|
||
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
|
||
const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
|
||
const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
|
||
const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
|
||
|
||
if (t === undefined) {
|
||
if (isLightMode) activateLightMode()
|
||
else if (isDarkMode) activateDarkMode()
|
||
else if (isNotSpecified || hasNoSupport) {
|
||
const now = new Date()
|
||
const hour = now.getHours()
|
||
const isNight = hour <= 8 || hour >= 22
|
||
isNight ? activateDarkMode() : activateLightMode()
|
||
}
|
||
window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
|
||
if (saveToLocal.get('theme') === undefined) {
|
||
e.matches ? activateDarkMode() : activateLightMode()
|
||
}
|
||
})
|
||
} else if (t === 'light') activateLightMode()
|
||
else activateDarkMode()
|
||
|
||
const asideStatus = saveToLocal.get('aside-status')
|
||
if (asideStatus !== undefined) {
|
||
if (asideStatus === 'hide') {
|
||
document.documentElement.classList.add('hide-aside')
|
||
} else {
|
||
document.documentElement.classList.remove('hide-aside')
|
||
}
|
||
}
|
||
|
||
const detectApple = () => {
|
||
if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
|
||
document.documentElement.classList.add('apple')
|
||
}
|
||
}
|
||
detectApple()
|
||
})(window)</script><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script type="text/javascript" src ="/js/welcome.js" ></script><script src="/js/sweetalert.js"></script><link rel="stylesheet" href="/css/sweetalert.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiperstyle.css" media="print" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.jpg" onerror="onerror=null;src='/img/loading.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">60</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">39</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div><br/><div class="menus_items"><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://www.tutorialspoint.com/compile_java8_online.php"><i class="fa-fw fas fa-code"></i><span> 代码</span></a></div><div class="menus_item"><a class="site-page" href="/notice/"><i class="fa-fw fas fa-stream"></i><span> 公告</span></a></div><div class="menus_item"><a class="site-page" href="/website/"><i class="fa-fw fas fa-list"></i><span> 网址</span></a></div><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="The Blog"><img class="site-icon" src="/img/logo.png"/><span class="site-name">The Blog</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://www.tutorialspoint.com/compile_java8_online.php"><i class="fa-fw fas fa-code"></i><span> 代码</span></a></div><div class="menus_item"><a class="site-page" href="/notice/"><i class="fa-fw fas fa-stream"></i><span> 公告</span></a></div><div class="menus_item"><a class="site-page" href="/website/"><i class="fa-fw fas fa-list"></i><span> 网址</span></a></div><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">阿里云对象存储OSS</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-04-22T12:54:02.000Z" title="发表于 2023-04-22 20:54:02">2023-04-22</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-09-11T23:36:48.000Z" title="更新于 2023-09-12 07:36:48">2023-09-12</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%90%8E%E7%AB%AF/">后端</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">4.1k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>19分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="阿里云对象存储OSS"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><h2 id="1-官网介绍"><a href="#1-官网介绍" class="headerlink" title="1.官网介绍"></a>1.官网介绍</h2><p> 阿里云对象存储OSS(Object Storage Service)是一款海量、安全、低成本、高可靠的云存储服务,可提供99.9999999999%(12个9)的数据持久性,99.995%的数据可用性。多种存储类型供选择,全面优化存储成本。OSS具有与平台无关的RESTful API接口,您可以在任何应用、任何时间、任何地点存储和访问任意类型的数据。您可以使用阿里云提供的API、SDK接口或者OSS迁移工具轻松地将海量数据移入或移出阿里云OSS。数据存储到阿里云OSS以后,您可以选择标准存储(Standard)作为移动应用、大型网站、图片分享或热点音视频的主要存储方式,也可以选择成本更低、存储期限更长的低频访问存储(Infrequent Access)、归档存储(Archive)、冷归档存储(Cold Archive)作为不经常访问数据的存储方式。</p>
|
||
<h2 id="2-使用"><a href="#2-使用" class="headerlink" title="2.使用"></a>2.使用</h2><h3 id="2-1-使用的前置准备"><a href="#2-1-使用的前置准备" class="headerlink" title="2.1 使用的前置准备"></a>2.1 使用的前置准备</h3><p>(1)申请阿里云账号<br>(2)实名认证<br>(3)开通“对象存储OSS”服务<br>(4)进入管理控制台</p>
|
||
<h3 id="2-2-创建Bucket"><a href="#2-2-创建Bucket" class="headerlink" title="2.2 创建Bucket"></a>2.2 创建Bucket</h3><p>点击创建Bucket</p>
|
||
<p><img src="/pictures/image-20230422210404204.png" alt="image-20230422210404204"></p>
|
||
<p>填写相关的信息</p>
|
||
<p><img src="/pictures/image-20230422211448747.png" alt="image-20230422211448747"></p>
|
||
<h3 id="2-3获取AccessKey保存备用"><a href="#2-3获取AccessKey保存备用" class="headerlink" title="2.3获取AccessKey保存备用"></a>2.3获取AccessKey保存备用</h3><p>AccessKey拥有对阿里云提供服务(对象存储、短信服务、视频点播服务等等)的控制权,要妥善保管</p>
|
||
<p><img src="/pictures/image-20230422211940485.png" alt="image-20230422211940485"></p>
|
||
<h3 id="2-4-查看开发文档"><a href="#2-4-查看开发文档" class="headerlink" title="2.4 查看开发文档"></a>2.4 查看开发文档</h3><p>刚开始不会使用的话,查看开发文档是入门的最好方式</p>
|
||
<p><img src="/pictures/image-20230422212508029.png" alt="image-20230422212508029"></p>
|
||
<h3 id="2-5-入门案例"><a href="#2-5-入门案例" class="headerlink" title="2.5 入门案例"></a>2.5 入门案例</h3><p>创建一个SpringBoot工程</p>
|
||
<h4 id="1-引入依赖"><a href="#1-引入依赖" class="headerlink" title="1.引入依赖"></a>1.引入依赖</h4><p>可以引入一个日期工具类<strong>joda-time</strong> 方便后面生成图片存储的路径</p>
|
||
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>com.aliyun.oss<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>aliyun-sdk-oss<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>3.15.1<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependency</span>></span></span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="2-修改配置文件"><a href="#2-修改配置文件" class="headerlink" title="2.修改配置文件"></a>2.修改配置文件</h4><p>入门案例中以下值会直接在代码中写死,先不从配置文件中获取</p>
|
||
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 地域节点 bucket概览中查看</span></span><br><span class="line"><span class="attr">aliyun.oss.endpoint</span>=<span class="string">oss-cn-beijing.aliyuncs.com</span></span><br><span class="line"><span class="comment"># accessKey</span></span><br><span class="line"><span class="attr">aliyun.oss.accessKeyId</span>=<span class="string">LTAI4G4SV6WtST7UYH77XXXX</span></span><br><span class="line"><span class="comment"># secret</span></span><br><span class="line"><span class="attr">aliyun.oss.secret</span>=<span class="string">X9KHNYgztNr9MI5Zp8JffXXXXXXXX</span></span><br><span class="line"><span class="comment"># bucket名</span></span><br><span class="line"><span class="attr">aliyun.oss.bucket</span>=<span class="string">yygh-atguigu</span></span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="3-创建读取配置文件的配置类"><a href="#3-创建读取配置文件的配置类" class="headerlink" title="3.创建读取配置文件的配置类"></a>3.创建读取配置文件的配置类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ConstantOssPropertiesUtils</span> <span class="keyword">implements</span> <span class="title class_">InitializingBean</span> {</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Value("${aliyun.oss.endpoint}")</span></span><br><span class="line"> <span class="keyword">private</span> String endpoint;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Value("${aliyun.oss.accessKeyId}")</span></span><br><span class="line"> <span class="keyword">private</span> String accessKeyId;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Value("${aliyun.oss.secret}")</span></span><br><span class="line"> <span class="keyword">private</span> String secret;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Value("${aliyun.oss.bucket}")</span></span><br><span class="line"> <span class="keyword">private</span> String bucket;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String EDNPOINT;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String ACCESS_KEY_ID;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String SECRECT;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String BUCKET;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">afterPropertiesSet</span><span class="params">()</span> <span class="keyword">throws</span> Exception {</span><br><span class="line"> EDNPOINT=endpoint;</span><br><span class="line"> ACCESS_KEY_ID=accessKeyId;</span><br><span class="line"> SECRECT=secret;</span><br><span class="line"> BUCKET=bucket;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="4-测试案例"><a href="#4-测试案例" class="headerlink" title="4.测试案例"></a>4.测试案例</h4><p>以下测试案例由官方开发文档提供</p>
|
||
<p>1.通过代码创建Bucket</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo</span> {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception {</span><br><span class="line"> <span class="comment">// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">endpoint</span> <span class="operator">=</span> <span class="string">"https://oss-cn-hangzhou.aliyuncs.com"</span>;</span><br><span class="line"> <span class="comment">// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">accessKeyId</span> <span class="operator">=</span> <span class="string">"yourAccessKeyId"</span>;</span><br><span class="line"> <span class="type">String</span> <span class="variable">accessKeySecret</span> <span class="operator">=</span> <span class="string">"yourAccessKeySecret"</span>;</span><br><span class="line"> <span class="comment">// 填写Bucket名称,例如examplebucket。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">bucketName</span> <span class="operator">=</span> <span class="string">"examplebucket"</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 创建OSSClient实例。</span></span><br><span class="line"> <span class="type">OSS</span> <span class="variable">ossClient</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">OSSClientBuilder</span>().build(endpoint, accessKeyId, accessKeySecret);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="comment">// 创建存储空间。</span></span><br><span class="line"> ossClient.createBucket(bucketName);</span><br><span class="line"></span><br><span class="line"> } <span class="keyword">catch</span> (OSSException oe) {</span><br><span class="line"> System.out.println(<span class="string">"Caught an OSSException, which means your request made it to OSS, "</span></span><br><span class="line"> + <span class="string">"but was rejected with an error response for some reason."</span>);</span><br><span class="line"> System.out.println(<span class="string">"Error Message:"</span> + oe.getErrorMessage());</span><br><span class="line"> System.out.println(<span class="string">"Error Code:"</span> + oe.getErrorCode());</span><br><span class="line"> System.out.println(<span class="string">"Request ID:"</span> + oe.getRequestId());</span><br><span class="line"> System.out.println(<span class="string">"Host ID:"</span> + oe.getHostId());</span><br><span class="line"> } <span class="keyword">catch</span> (ClientException ce) {</span><br><span class="line"> System.out.println(<span class="string">"Caught an ClientException, which means the client encountered "</span></span><br><span class="line"> + <span class="string">"a serious internal problem while trying to communicate with OSS, "</span></span><br><span class="line"> + <span class="string">"such as not being able to access the network."</span>);</span><br><span class="line"> System.out.println(<span class="string">"Error Message:"</span> + ce.getMessage());</span><br><span class="line"> } <span class="keyword">finally</span> {</span><br><span class="line"> <span class="keyword">if</span> (ossClient != <span class="literal">null</span>) {</span><br><span class="line"> ossClient.shutdown();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<p>2.上传文件</p>
|
||
<p>以下代码用于通过流式上传的方式将文件上传到OSS。</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> com.aliyun.oss.ClientException;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSS;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSSClientBuilder;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSSException;</span><br><span class="line"><span class="keyword">import</span> java.io.ByteArrayInputStream;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo</span> {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception {</span><br><span class="line"> <span class="comment">// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">endpoint</span> <span class="operator">=</span> <span class="string">"https://oss-cn-hangzhou.aliyuncs.com"</span>;</span><br><span class="line"> <span class="comment">// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">accessKeyId</span> <span class="operator">=</span> <span class="string">"yourAccessKeyId"</span>;</span><br><span class="line"> <span class="type">String</span> <span class="variable">accessKeySecret</span> <span class="operator">=</span> <span class="string">"yourAccessKeySecret"</span>;</span><br><span class="line"> <span class="comment">// 填写Bucket名称,例如examplebucket。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">bucketName</span> <span class="operator">=</span> <span class="string">"examplebucket"</span>;</span><br><span class="line"> <span class="comment">// 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">objectName</span> <span class="operator">=</span> <span class="string">"exampledir/exampleobject.txt"</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 创建OSSClient实例。</span></span><br><span class="line"> <span class="type">OSS</span> <span class="variable">ossClient</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">OSSClientBuilder</span>().build(endpoint, accessKeyId, accessKeySecret);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="type">String</span> <span class="variable">content</span> <span class="operator">=</span> <span class="string">"Hello OSS"</span>;</span><br><span class="line"> ossClient.putObject(bucketName, objectName, <span class="keyword">new</span> <span class="title class_">ByteArrayInputStream</span>(content.getBytes()));</span><br><span class="line"> } <span class="keyword">catch</span> (OSSException oe) {</span><br><span class="line"> System.out.println(<span class="string">"Caught an OSSException, which means your request made it to OSS, "</span></span><br><span class="line"> + <span class="string">"but was rejected with an error response for some reason."</span>);</span><br><span class="line"> System.out.println(<span class="string">"Error Message:"</span> + oe.getErrorMessage());</span><br><span class="line"> System.out.println(<span class="string">"Error Code:"</span> + oe.getErrorCode());</span><br><span class="line"> System.out.println(<span class="string">"Request ID:"</span> + oe.getRequestId());</span><br><span class="line"> System.out.println(<span class="string">"Host ID:"</span> + oe.getHostId());</span><br><span class="line"> } <span class="keyword">catch</span> (ClientException ce) {</span><br><span class="line"> System.out.println(<span class="string">"Caught an ClientException, which means the client encountered "</span></span><br><span class="line"> + <span class="string">"a serious internal problem while trying to communicate with OSS, "</span></span><br><span class="line"> + <span class="string">"such as not being able to access the network."</span>);</span><br><span class="line"> System.out.println(<span class="string">"Error Message:"</span> + ce.getMessage());</span><br><span class="line"> } <span class="keyword">finally</span> {</span><br><span class="line"> <span class="keyword">if</span> (ossClient != <span class="literal">null</span>) {</span><br><span class="line"> ossClient.shutdown();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<p>3.下载文件</p>
|
||
<p>以下代码用于通过流式下载方式从OSS下载文件。</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> com.aliyun.oss.ClientException;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSS;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSSClientBuilder;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSSException;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.model.OSSObject;</span><br><span class="line"><span class="keyword">import</span> java.io.BufferedReader;</span><br><span class="line"><span class="keyword">import</span> java.io.InputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.InputStreamReader;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo</span> {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception {</span><br><span class="line"> <span class="comment">// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">endpoint</span> <span class="operator">=</span> <span class="string">"https://oss-cn-hangzhou.aliyuncs.com"</span>;</span><br><span class="line"> <span class="comment">// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">accessKeyId</span> <span class="operator">=</span> <span class="string">"yourAccessKeyId"</span>;</span><br><span class="line"> <span class="type">String</span> <span class="variable">accessKeySecret</span> <span class="operator">=</span> <span class="string">"yourAccessKeySecret"</span>;</span><br><span class="line"> <span class="comment">// 填写Bucket名称,例如examplebucket。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">bucketName</span> <span class="operator">=</span> <span class="string">"examplebucket"</span>;</span><br><span class="line"> <span class="comment">// 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">objectName</span> <span class="operator">=</span> <span class="string">"exampledir/exampleobject.txt"</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 创建OSSClient实例。</span></span><br><span class="line"> <span class="type">OSS</span> <span class="variable">ossClient</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">OSSClientBuilder</span>().build(endpoint, accessKeyId, accessKeySecret);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="comment">// 调用ossClient.getObject返回一个OSSObject实例,该实例包含文件内容及文件元信息。</span></span><br><span class="line"> <span class="type">OSSObject</span> <span class="variable">ossObject</span> <span class="operator">=</span> ossClient.getObject(bucketName, objectName);</span><br><span class="line"> <span class="comment">// 调用ossObject.getObjectContent获取文件输入流,可读取此输入流获取其内容。</span></span><br><span class="line"> <span class="type">InputStream</span> <span class="variable">content</span> <span class="operator">=</span> ossObject.getObjectContent();</span><br><span class="line"> <span class="keyword">if</span> (content != <span class="literal">null</span>) {</span><br><span class="line"> <span class="type">BufferedReader</span> <span class="variable">reader</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedReader</span>(<span class="keyword">new</span> <span class="title class_">InputStreamReader</span>(content));</span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="type">String</span> <span class="variable">line</span> <span class="operator">=</span> reader.readLine();</span><br><span class="line"> <span class="keyword">if</span> (line == <span class="literal">null</span>) <span class="keyword">break</span>;</span><br><span class="line"> System.out.println(<span class="string">"\n"</span> + line);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。</span></span><br><span class="line"> content.close();</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">catch</span> (OSSException oe) {</span><br><span class="line"> System.out.println(<span class="string">"Caught an OSSException, which means your request made it to OSS, "</span></span><br><span class="line"> + <span class="string">"but was rejected with an error response for some reason."</span>);</span><br><span class="line"> System.out.println(<span class="string">"Error Message:"</span> + oe.getErrorMessage());</span><br><span class="line"> System.out.println(<span class="string">"Error Code:"</span> + oe.getErrorCode());</span><br><span class="line"> System.out.println(<span class="string">"Request ID:"</span> + oe.getRequestId());</span><br><span class="line"> System.out.println(<span class="string">"Host ID:"</span> + oe.getHostId());</span><br><span class="line"> } <span class="keyword">catch</span> (ClientException ce) {</span><br><span class="line"> System.out.println(<span class="string">"Caught an ClientException, which means the client encountered "</span></span><br><span class="line"> + <span class="string">"a serious internal problem while trying to communicate with OSS, "</span></span><br><span class="line"> + <span class="string">"such as not being able to access the network."</span>);</span><br><span class="line"> System.out.println(<span class="string">"Error Message:"</span> + ce.getMessage());</span><br><span class="line"> } <span class="keyword">finally</span> {</span><br><span class="line"> <span class="keyword">if</span> (ossClient != <span class="literal">null</span>) {</span><br><span class="line"> ossClient.shutdown();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<p>4.列举文件</p>
|
||
<p>以下代码用于列举examplebucket存储空间下的文件。默认列举100个文件。</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> com.aliyun.oss.ClientException;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSS;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSSClientBuilder;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSSException;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.model.OSSObjectSummary;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.model.ObjectListing;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo</span> {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception {</span><br><span class="line"> <span class="comment">// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">endpoint</span> <span class="operator">=</span> <span class="string">"https://oss-cn-hangzhou.aliyuncs.com"</span>;</span><br><span class="line"> <span class="comment">// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">accessKeyId</span> <span class="operator">=</span> <span class="string">"yourAccessKeyId"</span>;</span><br><span class="line"> <span class="type">String</span> <span class="variable">accessKeySecret</span> <span class="operator">=</span> <span class="string">"yourAccessKeySecret"</span>;</span><br><span class="line"> <span class="comment">// 填写Bucket名称,例如examplebucket。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">bucketName</span> <span class="operator">=</span> <span class="string">"examplebucket"</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 创建OSSClient实例。</span></span><br><span class="line"> <span class="type">OSS</span> <span class="variable">ossClient</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">OSSClientBuilder</span>().build(endpoint, accessKeyId, accessKeySecret);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="comment">// ossClient.listObjects返回ObjectListing实例,包含此次listObject请求的返回结果。</span></span><br><span class="line"> <span class="type">ObjectListing</span> <span class="variable">objectListing</span> <span class="operator">=</span> ossClient.listObjects(bucketName);</span><br><span class="line"> <span class="comment">// objectListing.getObjectSummaries获取所有文件的描述信息。</span></span><br><span class="line"> <span class="keyword">for</span> (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {</span><br><span class="line"> System.out.println(<span class="string">" - "</span> + objectSummary.getKey() + <span class="string">" "</span> +</span><br><span class="line"> <span class="string">"(size = "</span> + objectSummary.getSize() + <span class="string">")"</span>);</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">catch</span> (OSSException oe) {</span><br><span class="line"> System.out.println(<span class="string">"Caught an OSSException, which means your request made it to OSS, "</span></span><br><span class="line"> + <span class="string">"but was rejected with an error response for some reason."</span>);</span><br><span class="line"> System.out.println(<span class="string">"Error Message:"</span> + oe.getErrorMessage());</span><br><span class="line"> System.out.println(<span class="string">"Error Code:"</span> + oe.getErrorCode());</span><br><span class="line"> System.out.println(<span class="string">"Request ID:"</span> + oe.getRequestId());</span><br><span class="line"> System.out.println(<span class="string">"Host ID:"</span> + oe.getHostId());</span><br><span class="line"> } <span class="keyword">catch</span> (ClientException ce) {</span><br><span class="line"> System.out.println(<span class="string">"Caught an ClientException, which means the client encountered "</span></span><br><span class="line"> + <span class="string">"a serious internal problem while trying to communicate with OSS, "</span></span><br><span class="line"> + <span class="string">"such as not being able to access the network."</span>);</span><br><span class="line"> System.out.println(<span class="string">"Error Message:"</span> + ce.getMessage());</span><br><span class="line"> } <span class="keyword">finally</span> {</span><br><span class="line"> <span class="keyword">if</span> (ossClient != <span class="literal">null</span>) {</span><br><span class="line"> ossClient.shutdown();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<p>5.删除文件</p>
|
||
<p>以下代码用于删除指定文件。</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> com.aliyun.oss.ClientException;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSS;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSSClientBuilder;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSSException;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo</span> {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception {</span><br><span class="line"> <span class="comment">// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">endpoint</span> <span class="operator">=</span> <span class="string">"https://oss-cn-hangzhou.aliyuncs.com"</span>;</span><br><span class="line"> <span class="comment">// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">accessKeyId</span> <span class="operator">=</span> <span class="string">"yourAccessKeyId"</span>;</span><br><span class="line"> <span class="type">String</span> <span class="variable">accessKeySecret</span> <span class="operator">=</span> <span class="string">"yourAccessKeySecret"</span>;</span><br><span class="line"> <span class="comment">// 填写Bucket名称,例如examplebucket。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">bucketName</span> <span class="operator">=</span> <span class="string">"examplebucket"</span>;</span><br><span class="line"> <span class="comment">// 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">objectName</span> <span class="operator">=</span> <span class="string">"exampledir/exampleobject.txt"</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 创建OSSClient实例。</span></span><br><span class="line"> <span class="type">OSS</span> <span class="variable">ossClient</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">OSSClientBuilder</span>().build(endpoint, accessKeyId, accessKeySecret);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="comment">// 删除文件。</span></span><br><span class="line"> ossClient.deleteObject(bucketName, objectName);</span><br><span class="line"> } <span class="keyword">catch</span> (OSSException oe) {</span><br><span class="line"> System.out.println(<span class="string">"Caught an OSSException, which means your request made it to OSS, "</span></span><br><span class="line"> + <span class="string">"but was rejected with an error response for some reason."</span>);</span><br><span class="line"> System.out.println(<span class="string">"Error Message:"</span> + oe.getErrorMessage());</span><br><span class="line"> System.out.println(<span class="string">"Error Code:"</span> + oe.getErrorCode());</span><br><span class="line"> System.out.println(<span class="string">"Request ID:"</span> + oe.getRequestId());</span><br><span class="line"> System.out.println(<span class="string">"Host ID:"</span> + oe.getHostId());</span><br><span class="line"> } <span class="keyword">catch</span> (ClientException ce) {</span><br><span class="line"> System.out.println(<span class="string">"Caught an ClientException, which means the client encountered "</span></span><br><span class="line"> + <span class="string">"a serious internal problem while trying to communicate with OSS, "</span></span><br><span class="line"> + <span class="string">"such as not being able to access the network."</span>);</span><br><span class="line"> System.out.println(<span class="string">"Error Message:"</span> + ce.getMessage());</span><br><span class="line"> } <span class="keyword">finally</span> {</span><br><span class="line"> <span class="keyword">if</span> (ossClient != <span class="literal">null</span>) {</span><br><span class="line"> ossClient.shutdown();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="5-项目中使用OSS对象存储"><a href="#5-项目中使用OSS对象存储" class="headerlink" title="5.项目中使用OSS对象存储"></a>5.项目中使用OSS对象存储</h4><h5 id="1-文件的上传操作"><a href="#1-文件的上传操作" class="headerlink" title="1.文件的上传操作"></a>1.文件的上传操作</h5><p>controller层</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> com.atguigu.yygh.common.result.Result;</span><br><span class="line"><span class="keyword">import</span> com.atguigu.yygh.oss.service.OSSService;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.PostMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RestController;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.multipart.MultipartFile;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/4/22</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 对象存储的控制器方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping("/api/oss/file")</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">OSSController</span> {</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Autowired</span></span><br><span class="line"> <span class="keyword">private</span> OSSService ossService;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 上传文件到阿里云</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@PostMapping("/fileUpload")</span></span><br><span class="line"> <span class="keyword">public</span> Result <span class="title function_">fileUpload</span><span class="params">(MultipartFile file)</span>{</span><br><span class="line"> <span class="comment">//上传文件,返回文件的url地址</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">url</span> <span class="operator">=</span> ossService.upload(file);</span><br><span class="line"> <span class="keyword">return</span> Result.ok(url);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<p>service层</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> com.aliyun.oss.ClientException;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSS;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSSClientBuilder;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSSException;</span><br><span class="line"><span class="keyword">import</span> com.atguigu.yygh.oss.service.OSSService;</span><br><span class="line"><span class="keyword">import</span> com.atguigu.yygh.oss.utils.ConstantOssPropertiesUtils;</span><br><span class="line"><span class="keyword">import</span> org.joda.time.DateTime;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Service;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.multipart.MultipartFile;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.ByteArrayInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.io.InputStream;</span><br><span class="line"><span class="keyword">import</span> java.util.UUID;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/4/22</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> OSS文件相关操作</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">OSSServiceImpl</span> <span class="keyword">implements</span> <span class="title class_">OSSService</span> {</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 上传文件</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> file 文件</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 文件的地址</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> String <span class="title function_">upload</span><span class="params">(MultipartFile file)</span> {</span><br><span class="line"> <span class="comment">// Endpoint,地域节点信息</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">endpoint</span> <span class="operator">=</span> ConstantOssPropertiesUtils.EDNPOINT;</span><br><span class="line"> <span class="comment">// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">accessKeyId</span> <span class="operator">=</span> ConstantOssPropertiesUtils.ACCESS_KEY_ID;</span><br><span class="line"> <span class="type">String</span> <span class="variable">accessKeySecret</span> <span class="operator">=</span> ConstantOssPropertiesUtils.SECRECT;</span><br><span class="line"> <span class="comment">// Bucket名称</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">bucketName</span> <span class="operator">=</span> ConstantOssPropertiesUtils.BUCKET;</span><br><span class="line"> <span class="comment">// 创建OSSClient实例。</span></span><br><span class="line"> <span class="type">OSS</span> <span class="variable">ossClient</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">OSSClientBuilder</span>().build(endpoint, accessKeyId, accessKeySecret);</span><br><span class="line"> <span class="comment">//生成文件名 生成文件名的策略是日期/文件名.jpg 文件名由UUID随机生成</span></span><br><span class="line"> <span class="comment">//获取原始的文件名</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">originalFilename</span> <span class="operator">=</span> file.getOriginalFilename();</span><br><span class="line"> <span class="comment">//分割出扩展名</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">suffixFilename</span> <span class="operator">=</span> originalFilename.substring(originalFilename.lastIndexOf(<span class="string">"."</span>));</span><br><span class="line"> <span class="comment">//得到随机的文件名</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">prefixFilename</span> <span class="operator">=</span> UUID.randomUUID().toString();</span><br><span class="line"> <span class="comment">//得到文件名</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">filename</span> <span class="operator">=</span> prefixFilename + suffixFilename;</span><br><span class="line"> <span class="comment">//将文件按照日期进行分类 日期/文件名.jpg</span></span><br><span class="line"> <span class="comment">//获取当前的日期 使用时间作为目录</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">filePath</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DateTime</span>().toString(<span class="string">"yyyy/MM/dd"</span>);</span><br><span class="line"> <span class="comment">//得到最终的文件名</span></span><br><span class="line"> filename = filePath + <span class="string">"/"</span> + filename;</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="comment">//获取上传文件的流</span></span><br><span class="line"> <span class="type">InputStream</span> <span class="variable">inputStream</span> <span class="operator">=</span> file.getInputStream();</span><br><span class="line"> <span class="comment">//上传文件</span></span><br><span class="line"> ossClient.putObject(bucketName, filename, inputStream);</span><br><span class="line"> <span class="comment">//拼接文件的url地址并返回</span></span><br><span class="line"> <span class="comment">//https://edu-guli-0306.oss-cn-beijing.aliyuncs.com/81ea0d8d-0e52-4749-be58-6292410deec6.jpg</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">"https://"</span> + bucketName + <span class="string">"."</span> + endpoint + <span class="string">"/"</span> + filename;</span><br><span class="line"> } <span class="keyword">catch</span> (IOException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line"> } <span class="keyword">finally</span> {</span><br><span class="line"> <span class="keyword">if</span> (ossClient != <span class="literal">null</span>) {</span><br><span class="line"> ossClient.shutdown();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<h2 id="3-Aliyun-Spring-Boot-OSS"><a href="#3-Aliyun-Spring-Boot-OSS" class="headerlink" title="3.Aliyun Spring Boot OSS"></a>3.Aliyun Spring Boot OSS</h2><p><strong>[使用文档](<a target="_blank" rel="noopener" href="https://github.com/alibaba/aliyun-spring-boot/blob/master/aliyun-spring-boot-samples/aliyun-oss-spring-boot-sample/README-zh.md">aliyun-spring-boot/README-zh.md at master · alibaba/aliyun-spring-boot · GitHub</a>)</strong></p>
|
||
<p>相比与上面的Demo,这个Demo更加的简便,编写的代码比较少</p>
|
||
<p><strong>项目说明</strong></p>
|
||
<p> 如果您的应用是 Spring Cloud 应用,且需要使用阿里云的 OSS 服务进行云端的文件存储,例如电商业务中常见的商品图片存储,那么您可以使用 OSS starter 完成 Spring Cloud 应用的对象存储。</p>
|
||
<p>阿里云对象存储服务(Object Storage Service,简称 OSS),是阿里云提供的海量、安全、低成本、高可靠的云存储服务。您可以在任何应用、任何时间、任何地点存储和访问任意类型的数据。更多 OSS 相关的信息,请参考 <a target="_blank" rel="noopener" href="https://www.aliyun.com/product/oss">OSS官网</a>。</p>
|
||
<h3 id="1-引入依赖-1"><a href="#1-引入依赖-1" class="headerlink" title="1.引入依赖"></a>1.引入依赖</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!-- 阿里云的对象存储的依赖--></span></span><br><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>com.alibaba.cloud<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>spring-cloud-starter-alicloud-oss<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>2.1.0.RELEASE<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">type</span>></span>pom<span class="tag"></<span class="name">type</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependency</span>></span></span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="2-在配置文件中增加配置"><a href="#2-在配置文件中增加配置" class="headerlink" title="2.在配置文件中增加配置"></a>2.在配置文件中增加配置</h3><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring.cloud.alicloud.access-key</span>=<span class="string">************</span></span><br><span class="line"><span class="attr">spring.cloud.alicloud.secret-key</span>=<span class="string">************</span></span><br><span class="line"><span class="attr">spring.cloud.alicloud.oss.endpoint</span>=<span class="string">************</span></span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="3-测试使用"><a href="#3-测试使用" class="headerlink" title="3.测试使用"></a>3.测试使用</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Autowired</span></span><br><span class="line">OSSClient ossClient;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 测试使用阿里云上传视频</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">testUpload</span><span class="params">()</span> <span class="keyword">throws</span> FileNotFoundException {</span><br><span class="line"> <span class="type">String</span> <span class="variable">bucketName</span> <span class="operator">=</span> <span class="string">"gulimall-0611"</span>;</span><br><span class="line"> <span class="type">String</span> <span class="variable">objectName</span> <span class="operator">=</span> <span class="string">"test1.jpg"</span>;</span><br><span class="line"> <span class="type">InputStream</span> <span class="variable">inputStream</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">"C:\\Gong\\data\\test.jpg"</span>);</span><br><span class="line"> <span class="comment">// 创建PutObjectRequest对象。</span></span><br><span class="line"> ossClient.putObject(bucketName,objectName,inputStream);</span><br><span class="line"> ossClient.shutdown();</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="4-在项目中使用OSS进行文件的上传和下载的操作"><a href="#4-在项目中使用OSS进行文件的上传和下载的操作" class="headerlink" title="4.在项目中使用OSS进行文件的上传和下载的操作"></a>4.在项目中使用OSS进行文件的上传和下载的操作</h3><p><strong>服务端签名直传</strong> 使用这种方式可以减轻本地服务器的压力</p>
|
||
<p>参考文档:<a target="_blank" rel="noopener" href="https://help.aliyun.com/document_detail/91868.htm?spm=a2c4g.31927.0.0.44193d70tJDLJb#concept-ahk-rfz-2fb">https://help.aliyun.com/document_detail/91868.htm?spm=a2c4g.31927.0.0.44193d70tJDLJb#concept-ahk-rfz-2fb</a></p>
|
||
<img src="/pictures/image-20230611214209712.png" alt="image-20230611214209712" style="zoom:150%;" />
|
||
|
||
<h4 id="4-1-获取文件上传相关的签名信息"><a href="#4-1-获取文件上传相关的签名信息" class="headerlink" title="4.1 获取文件上传相关的签名信息"></a>4.1 获取文件上传相关的签名信息</h4><p>获取签名信息的接口</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.gulimall.thirdparty.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSS;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.OSSClientBuilder;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.common.utils.BinaryUtil;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.model.MatchMode;</span><br><span class="line"><span class="keyword">import</span> com.aliyun.oss.model.PolicyConditions;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Value;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RestController;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"><span class="keyword">import</span> java.util.LinkedHashMap;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Jason Gong</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/6/11</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 阿里云对象存储的控制器方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">OSSController</span> {</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Autowired</span></span><br><span class="line"> OSS ossClient;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Value("${spring.cloud.alicloud.oss.endpoint}")</span></span><br><span class="line"> <span class="keyword">private</span> String endpoint;</span><br><span class="line"> <span class="meta">@Value("${spring.cloud.alicloud.oss.bucket}")</span></span><br><span class="line"> <span class="keyword">private</span> String bucket;</span><br><span class="line"> <span class="meta">@Value("${spring.cloud.alicloud.access-key}")</span></span><br><span class="line"> <span class="keyword">private</span> String accessId;</span><br><span class="line"> <span class="meta">@Value("${spring.cloud.alicloud.secret-key}")</span></span><br><span class="line"> <span class="keyword">private</span> String accessKey;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/oss/policy")</span></span><br><span class="line"> <span class="keyword">public</span> Map<String, String> <span class="title function_">policy</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">// 填写Host地址,格式为https://bucketname.endpoint。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">host</span> <span class="operator">=</span> <span class="string">"https://"</span>+bucket+<span class="string">"."</span>+endpoint;</span><br><span class="line"> <span class="comment">// 设置上传回调URL,即回调服务器地址,用于处理应用服务器与OSS之间的通信。OSS会在文件上传完成后,把文件上传信息通过此回调URL发送给应用服务器。</span></span><br><span class="line"> <span class="comment">//String callbackUrl = "https://192.168.0.0:8888";</span></span><br><span class="line"> <span class="comment">// 设置上传到OSS文件的前缀,可置空此项。置空后,文件将上传至Bucket的根目录下。</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">format</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleDateFormat</span>(<span class="string">"yyyy/MM/dd"</span>).format(<span class="keyword">new</span> <span class="title class_">Date</span>());</span><br><span class="line"> <span class="type">String</span> <span class="variable">dir</span> <span class="operator">=</span> format+<span class="string">"/"</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 创建ossClient实例。</span></span><br><span class="line"> <span class="type">OSS</span> <span class="variable">ossClient</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">OSSClientBuilder</span>().build(endpoint, accessId, accessKey);</span><br><span class="line"> Map<String, String> respMap = <span class="literal">null</span>;</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="type">long</span> <span class="variable">expireTime</span> <span class="operator">=</span> <span class="number">30</span>;</span><br><span class="line"> <span class="type">long</span> <span class="variable">expireEndTime</span> <span class="operator">=</span> System.currentTimeMillis() + expireTime * <span class="number">1000</span>;</span><br><span class="line"> <span class="type">Date</span> <span class="variable">expiration</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Date</span>(expireEndTime);</span><br><span class="line"> <span class="type">PolicyConditions</span> <span class="variable">policyConds</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">PolicyConditions</span>();</span><br><span class="line"> policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, <span class="number">0</span>, <span class="number">1048576000</span>);</span><br><span class="line"> policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);</span><br><span class="line"></span><br><span class="line"> <span class="type">String</span> <span class="variable">postPolicy</span> <span class="operator">=</span> ossClient.generatePostPolicy(expiration, policyConds);</span><br><span class="line"> <span class="type">byte</span>[] binaryData = postPolicy.getBytes(<span class="string">"utf-8"</span>);</span><br><span class="line"> <span class="type">String</span> <span class="variable">encodedPolicy</span> <span class="operator">=</span> BinaryUtil.toBase64String(binaryData);</span><br><span class="line"> <span class="type">String</span> <span class="variable">postSignature</span> <span class="operator">=</span> ossClient.calculatePostSignature(postPolicy);</span><br><span class="line"></span><br><span class="line"> respMap = <span class="keyword">new</span> <span class="title class_">LinkedHashMap</span><String, String>();</span><br><span class="line"> respMap.put(<span class="string">"accessId"</span>, accessId);</span><br><span class="line"> respMap.put(<span class="string">"policy"</span>, encodedPolicy);</span><br><span class="line"> respMap.put(<span class="string">"signature"</span>, postSignature);</span><br><span class="line"> respMap.put(<span class="string">"dir"</span>, dir);</span><br><span class="line"> respMap.put(<span class="string">"host"</span>, host);</span><br><span class="line"> respMap.put(<span class="string">"expire"</span>, String.valueOf(expireEndTime / <span class="number">1000</span>));</span><br><span class="line"> <span class="comment">// respMap.put("expire", formatISO8601Date(expiration));</span></span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> System.out.println(e.getMessage());</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> respMap;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<p>访问接口之后返回的数据格式</p>
|
||
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"accessId"</span><span class="punctuation">:</span> <span class="string">"LTAI5tDDwk18w2S7w1KF24oT"</span><span class="punctuation">,</span><span class="comment">//access-key</span></span><br><span class="line"> <span class="attr">"policy"</span><span class="punctuation">:</span> <span class="string">"eyJleHBpcmF0aW9uIjoiMjAyMy0wNi0xMVQxNDoxNzozMi43OTdaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCIyMDIzLzA2LzExLyJdXX0="</span><span class="punctuation">,</span><span class="comment">//策略,加密之后的结果</span></span><br><span class="line"> <span class="attr">"signature"</span><span class="punctuation">:</span> <span class="string">"n67QffiYHceO2Hf7g8MDXgWsfpw="</span><span class="punctuation">,</span><span class="comment">//签名</span></span><br><span class="line"> <span class="attr">"dir"</span><span class="punctuation">:</span> <span class="string">"2023/06/11/"</span><span class="punctuation">,</span><span class="comment">//上传到的文件的地址</span></span><br><span class="line"> <span class="attr">"host"</span><span class="punctuation">:</span> <span class="string">"https://gulimall-0611.oss-cn-beijing.aliyuncs.com"</span><span class="punctuation">,</span><span class="comment">//上传到的主机的</span></span><br><span class="line"> <span class="attr">"expire"</span><span class="punctuation">:</span> <span class="string">"1686493052"</span><span class="comment">//过期时间</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
|
||
|
||
<p>前端相关的代码</p>
|
||
<p><strong>前端相关的代码可以借鉴一下谷粒商城的前端上传文件的组件,里面封装了多文件上传和单文件上传的功能</strong></p>
|
||
<p><img src="/pictures/image-20230612111805518.png" alt="image-20230612111805518"></p>
|
||
<p>出现跨域的问题</p>
|
||
<p><img src="/pictures/image-20230612114314757.png" alt="image-20230612114314757"></p>
|
||
<p>解决方法</p>
|
||
<p><img src="/pictures/image-20230612114238391.png" alt="image-20230612114238391"></p>
|
||
</article><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E4%BA%91%E8%AE%A1%E7%AE%97/">云计算</a></div><div class="post_share"><div class="social-share" data-image="/img/9.png" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="/cdn/css/share.min.css" media="print" onload="this.media='all'"><script src="/cdn/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><br/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="far fa-comment-alt fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="gitalk-container"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/loading.gif'" alt="avatar"/></div><div class="author-info__name">Jason</div><div class="author-info__description">Debug the World!</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">60</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">39</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div><a id="card-info-btn"><i class="fab fa-microsoft"></i><span>Ctrl + D 收藏</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/JasonsGong" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=2602183349&website=www.oicqzone.com" target="_blank" title="QQ"><i class="fab fa-qq"></i></a><a class="social-icon" href="mailto:2602183349@qq.com" target="_blank" title="Email"><i class="fas fa-envelope-open-text"></i></a><a class="social-icon" href="https://github.com/JasonsGong?tab=repositories" target="_blank" title="代码仓库"><i class="fas fa-database"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">本网站是静态网站,更新页面资源请使用Ctrl+F5;若网站内文章对你有帮助,请使用Ctrl+D收藏该网站!</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E5%AE%98%E7%BD%91%E4%BB%8B%E7%BB%8D"><span class="toc-text">1.官网介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E4%BD%BF%E7%94%A8"><span class="toc-text">2.使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-%E4%BD%BF%E7%94%A8%E7%9A%84%E5%89%8D%E7%BD%AE%E5%87%86%E5%A4%87"><span class="toc-text">2.1 使用的前置准备</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-%E5%88%9B%E5%BB%BABucket"><span class="toc-text">2.2 创建Bucket</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3%E8%8E%B7%E5%8F%96AccessKey%E4%BF%9D%E5%AD%98%E5%A4%87%E7%94%A8"><span class="toc-text">2.3获取AccessKey保存备用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4-%E6%9F%A5%E7%9C%8B%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3"><span class="toc-text">2.4 查看开发文档</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-5-%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B"><span class="toc-text">2.5 入门案例</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E5%BC%95%E5%85%A5%E4%BE%9D%E8%B5%96"><span class="toc-text">1.引入依赖</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E4%BF%AE%E6%94%B9%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-text">2.修改配置文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E5%88%9B%E5%BB%BA%E8%AF%BB%E5%8F%96%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E7%9A%84%E9%85%8D%E7%BD%AE%E7%B1%BB"><span class="toc-text">3.创建读取配置文件的配置类</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-%E6%B5%8B%E8%AF%95%E6%A1%88%E4%BE%8B"><span class="toc-text">4.测试案例</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-%E9%A1%B9%E7%9B%AE%E4%B8%AD%E4%BD%BF%E7%94%A8OSS%E5%AF%B9%E8%B1%A1%E5%AD%98%E5%82%A8"><span class="toc-text">5.项目中使用OSS对象存储</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-%E6%96%87%E4%BB%B6%E7%9A%84%E4%B8%8A%E4%BC%A0%E6%93%8D%E4%BD%9C"><span class="toc-text">1.文件的上传操作</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-Aliyun-Spring-Boot-OSS"><span class="toc-text">3.Aliyun Spring Boot OSS</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E5%BC%95%E5%85%A5%E4%BE%9D%E8%B5%96-1"><span class="toc-text">1.引入依赖</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%9C%A8%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%B8%AD%E5%A2%9E%E5%8A%A0%E9%85%8D%E7%BD%AE"><span class="toc-text">2.在配置文件中增加配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E6%B5%8B%E8%AF%95%E4%BD%BF%E7%94%A8"><span class="toc-text">3.测试使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E5%9C%A8%E9%A1%B9%E7%9B%AE%E4%B8%AD%E4%BD%BF%E7%94%A8OSS%E8%BF%9B%E8%A1%8C%E6%96%87%E4%BB%B6%E7%9A%84%E4%B8%8A%E4%BC%A0%E5%92%8C%E4%B8%8B%E8%BD%BD%E7%9A%84%E6%93%8D%E4%BD%9C"><span class="toc-text">4.在项目中使用OSS进行文件的上传和下载的操作</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-1-%E8%8E%B7%E5%8F%96%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E7%9B%B8%E5%85%B3%E7%9A%84%E7%AD%BE%E5%90%8D%E4%BF%A1%E6%81%AF"><span class="toc-text">4.1 获取文件上传相关的签名信息</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最近更新</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/8957.html" title="Linux从入门到进阶"><img src="/img/8.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Linux从入门到进阶"/></a><div class="content"><a class="title" href="/posts/8957.html" title="Linux从入门到进阶">Linux从入门到进阶</a><time datetime="2024-05-10T05:31:11.691Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/35630.html" title="接口测试工具"><img src="/img/2.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="接口测试工具"/></a><div class="content"><a class="title" href="/posts/35630.html" title="接口测试工具">接口测试工具</a><time datetime="2024-05-10T05:31:11.686Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/32246.html" title="SpringBoot中整合Swagger2"><img src="/img/3.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="SpringBoot中整合Swagger2"/></a><div class="content"><a class="title" href="/posts/32246.html" title="SpringBoot中整合Swagger2">SpringBoot中整合Swagger2</a><time datetime="2024-05-10T05:31:11.681Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/19306.html" title="Docker容器化技术"><img src="/img/1.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Docker容器化技术"/></a><div class="content"><a class="title" href="/posts/19306.html" title="Docker容器化技术">Docker容器化技术</a><time datetime="2024-05-10T05:31:11.675Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/20683.html" title="Linux中开发环境的搭建"><img src="/img/8.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Linux中开发环境的搭建"/></a><div class="content"><a class="title" href="/posts/20683.html" title="Linux中开发环境的搭建">Linux中开发环境的搭建</a><time datetime="2024-05-10T05:31:11.669Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comment-alt"></i></a><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/cdn/js/medium-zoom.min.js"></script><script src="/cdn/js/instantpage.min.js" type="module"></script><script src="/cdn/js/snackbar.min.js"></script><div class="js-pjax"><script>function loadGitalk () {
|
||
function initGitalk () {
|
||
var gitalk = new Gitalk(Object.assign({
|
||
clientID: '00fb27b1e484536359c2',
|
||
clientSecret: 'be41a12281c68b6e228d1a27e8d08aeb91541145',
|
||
repo: 'BlogComment',
|
||
owner: 'JasonsGong',
|
||
admin: ['JasonsGong'],
|
||
id: '10de71375eebd4fc9dfa33d1f502b99a',
|
||
updateCountCallback: commentCount
|
||
},null))
|
||
|
||
gitalk.render('gitalk-container')
|
||
}
|
||
|
||
if (typeof Gitalk === 'function') initGitalk()
|
||
else {
|
||
getCSS('/cdn/css/gitalk.min.css')
|
||
getScript('/cdn/js/gitalk.min.js').then(initGitalk)
|
||
}
|
||
}
|
||
|
||
function commentCount(n){
|
||
let isCommentCount = document.querySelector('#post-meta .gitalk-comment-count')
|
||
if (isCommentCount) {
|
||
isCommentCount.textContent= n
|
||
}
|
||
}
|
||
|
||
if ('Gitalk' === 'Gitalk' || !true) {
|
||
if (true) btf.loadComment(document.getElementById('gitalk-container'), loadGitalk)
|
||
else loadGitalk()
|
||
} else {
|
||
function loadOtherComment () {
|
||
loadGitalk()
|
||
}
|
||
}</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span> 数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><br/><div class="no-result" id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div><!-- hexo injector body_end start --><script data-pjax>
|
||
function butterfly_swiper_injector_config(){
|
||
var parent_div_git = document.getElementById('recent-posts');
|
||
var item_html = '<div class="recent-post-item" style="height: auto;width: 100%"><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms;"><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/19306.html" alt=""><img width="48" height="48" src="/img/1.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-04-21</span><a class="blog-slider__title" href="posts/19306.html" alt="">Docker容器化技术</a><div class="blog-slider__text">Docker</div><a class="blog-slider__button" href="posts/19306.html" alt="">详情 </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/47003.html" alt=""><img width="48" height="48" src="/img/5.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-03-10</span><a class="blog-slider__title" href="posts/47003.html" alt="">常用正则表达式大全</a><div class="blog-slider__text">正则表达式</div><a class="blog-slider__button" href="posts/47003.html" alt="">详情 </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/20683.html" alt=""><img width="48" height="48" src="/img/8.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-06-05</span><a class="blog-slider__title" href="posts/20683.html" alt="">Linux中开发环境的搭建</a><div class="blog-slider__text">环境搭建</div><a class="blog-slider__button" href="posts/20683.html" alt="">详情 </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/63333.html" alt=""><img width="48" height="48" src="/img/10.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-06-03</span><a class="blog-slider__title" href="posts/63333.html" alt="">开发环境的搭建</a><div class="blog-slider__text">环境搭建</div><a class="blog-slider__button" href="posts/63333.html" alt="">详情 </a></div></div></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div></div></div>';
|
||
if (parent_div_git !== null && typeof parent_div_git !== 'undefined') {
|
||
parent_div_git.insertAdjacentHTML("afterbegin",item_html)
|
||
}
|
||
}
|
||
var elist = 'undefined'.split(',');
|
||
var cpage = location.pathname;
|
||
var epage = 'all';
|
||
var flag = 0;
|
||
|
||
for (var i=0;i<elist.length;i++){
|
||
if (cpage.includes(elist[i])){
|
||
flag++;
|
||
}
|
||
}
|
||
|
||
if ((epage ==='all')&&(flag == 0)){
|
||
butterfly_swiper_injector_config();
|
||
}
|
||
else if (epage === cpage){
|
||
butterfly_swiper_injector_config();
|
||
}
|
||
</script><script defer src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.js"></script><script defer data-pjax src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper_init.js"></script><!-- hexo injector body_end end --></body></html> |