当前位置: 首页 > news >正文

优秀网站大全最彻底的手机优化软件

优秀网站大全,最彻底的手机优化软件,湛江找人做网站排名,wordpress 文章内容模版官方文档 https://help.aliyun.com/zh/oss/developer-reference/java 准备工作 windows安装好JDK,这里使用JDK1.8为例 windows安装好IDEA,这里使用IDEA2022 登录阿里云控制台,通过免费试用OSS或开通OSS 步骤 配置访问凭证 有临时和长期…

官方文档

https://help.aliyun.com/zh/oss/developer-reference/java

准备工作

  • windows安装好JDK,这里使用JDK1.8为例

  • windows安装好IDEA,这里使用IDEA2022

  • 登录阿里云控制台,通过免费试用OSS或开通OSS

步骤

配置访问凭证

有临时和长期访问凭证,简单起见,这里使用长期访问凭证。

配置RAM用户的访问密钥

1.创建RAM用户

登录阿里云,搜索访问控制,进入RAM 访问控制
在这里插入图片描述

在RAM 访问控制中,创建RAM用户
在这里插入图片描述
填写用户信息,勾选相关访问方式,点击确定
在这里插入图片描述
安全验证,根据提示进行其中一个验证。
在这里插入图片描述
得到RAM用户的相关信息,例如登录名称、登录密码、AccessKey ID和Access Secret
在这里插入图片描述

点击用户,找到刚创建的RAM用户一行,点击添加权限

在这里插入图片描述
添加AliyunOSSFullAccess权限
在这里插入图片描述

2.配置RAM用户的访问密钥。

有两种方式配置RAM用户的访问密钥

  • 设置环境变量
  • 代码嵌入
    把AccessKey ID和Access Secret设置在环境变量里比直接代码嵌入的方式更加安全些,两种方式选择其中之一即可。
设置环境变量

打开cmd命令行。

执行以下命令配置RAM用户的访问密钥。

set OSS_ACCESS_KEY_ID=LTAI4GDty8ab9W4Y1D****
set OSS_ACCESS_KEY_SECRET=IrVTNZNy5yQelTETg0cZML3TQn**** 

注意: OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET修改为对应AccessKey ID和Access Secret的值。
在这里插入图片描述

执行以下命令以使更改生效。

setx OSS_ACCESS_KEY_ID "%OSS_ACCESS_KEY_ID%"
setx OSS_ACCESS_KEY_SECRET "%OSS_ACCESS_KEY_SECRET%"

这个过程其实是往用户的环境变量里设置相应的值(用图形界面设置也一样)
在这里插入图片描述

执行以下命令验证环境变量配置。

echo %OSS_ACCESS_KEY_ID%
echo %OSS_ACCESS_KEY_SECRET%

成功返回示例如下:

LTAI4GDty8ab9W4Y1D****
IrVTNZNy5yQelTETg0cZML3TQn**** 

从环境变量中获取RAM用户的访问密钥。

// 使用环境变量中获取的RAM用户的访问密钥配置访问凭证。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

注意:设置环境变量后,如果之前已经打开IDEA,后面使用代码去获取环境变量时,需要重启IDEA,才能获取到新的环境变量设置。否则获取到的环境变量为null

代码嵌入
// RAM用户的访问密钥(AccessKey ID和AccessKey Secret)。
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// 使用代码嵌入的RAM用户的访问密钥配置访问凭证。
CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);

代码开发

1. 创建maven工程

打开IDEA->点击File->New Project
在这里插入图片描述

2. 添加sdk依赖
    <dependencies><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.15.1</version></dependency></dependencies>

刷新依赖

3. 功能代码开发

使用SDK API实现创建Bucket、列举Bucket文件、上传文件、下载文件、删除文件。代码参考官方帮助文档。
代码结构如下:
在这里插入图片描述

创建Bucket
package org.example;import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;public class CreateBucketDemo {public static void main(String[] args) throws Exception {// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。String endpoint = "https://oss-cn-shenzhen.aliyuncs.com";// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();// 填写Bucket名称,例如examplebucket。String bucketName = "examplebucket-test-test900";// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);try {// 创建存储空间。ossClient.createBucket(bucketName);} catch (OSSException oe) {System.out.println("Caught an OSSException, which means your request made it to OSS, "+ "but was rejected with an error response for some reason.");System.out.println("Error Message:" + oe.getErrorMessage());System.out.println("Error Code:" + oe.getErrorCode());System.out.println("Request ID:" + oe.getRequestId());System.out.println("Host ID:" + oe.getHostId());} catch (ClientException ce) {System.out.println("Caught an ClientException, which means the client encountered "+ "a serious internal problem while trying to communicate with OSS, "+ "such as not being able to access the network.");System.out.println("Error Message:" + ce.getMessage());} finally {if (ossClient != null) {ossClient.shutdown();}}}
}

*注意:修改endpointbucketName bucketName 不能有重复。

运行后,在阿里云控制台查看到bucket创建成功如下
在这里插入图片描述
可以手动上传一些文件到bucket里,点击Bucket名称
在这里插入图片描述
进入bucket文件列表页面,通过上传文件创建目录等方式让Bucket里得到一些目录和文件。
在这里插入图片描述

列举Bucket文件
package org.example;import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;public class ListFileDemo {public static void main(String[] args) throws Exception {// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。String endpoint = "https://oss-cn-shenzhen.aliyuncs.com";// 嵌入代码// RAM用户的访问密钥(AccessKey ID和AccessKey Secret)。注意修改xxx的值String accessKeyId = "xxx";String accessKeySecret = "xxx";// 使用代码嵌入的RAM用户的访问密钥配置访问凭证。CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
//        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();// 填写Bucket名称,例如examplebucket。String bucketName = "examplebucket-test-test900";// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);try {// ossClient.listObjects返回ObjectListing实例,包含此次listObject请求的返回结果。ObjectListing objectListing = ossClient.listObjects(bucketName);// objectListing.getObjectSummaries获取所有文件的描述信息。for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {System.out.println(" - " + objectSummary.getKey() + "  " +"(size = " + objectSummary.getSize() + ")");}} catch (OSSException oe) {System.out.println("Caught an OSSException, which means your request made it to OSS, "+ "but was rejected with an error response for some reason.");System.out.println("Error Message:" + oe.getErrorMessage());System.out.println("Error Code:" + oe.getErrorCode());System.out.println("Request ID:" + oe.getRequestId());System.out.println("Host ID:" + oe.getHostId());} catch (ClientException ce) {System.out.println("Caught an ClientException, which means the client encountered "+ "a serious internal problem while trying to communicate with OSS, "+ "such as not being able to access the network.");System.out.println("Error Message:" + ce.getMessage());} finally {if (ossClient != null) {ossClient.shutdown();}}}
}

使用代码嵌入的方式得到RAM用户的访问密钥。
注意修改accessKeyIdaccessKeySecret的值。
运行代码能看到一些文件
在这里插入图片描述

上传文件
package org.example;import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import java.io.ByteArrayInputStream;public class UploadFileDemo {public static void main(String[] args) throws Exception {// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。String endpoint = "https://oss-cn-shenzhen.aliyuncs.com";// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();// 填写Bucket名称,例如examplebucket。String bucketName = "examplebucket-test-test900";// 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。String objectName = "file/uploadobject1.txt";// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);try {String content = "Hello OSS";ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));} catch (OSSException oe) {System.out.println("Caught an OSSException, which means your request made it to OSS, "+ "but was rejected with an error response for some reason.");System.out.println("Error Message:" + oe.getErrorMessage());System.out.println("Error Code:" + oe.getErrorCode());System.out.println("Request ID:" + oe.getRequestId());System.out.println("Host ID:" + oe.getHostId());} catch (ClientException ce) {System.out.println("Caught an ClientException, which means the client encountered "+ "a serious internal problem while trying to communicate with OSS, "+ "such as not being able to access the network.");System.out.println("Error Message:" + ce.getMessage());} finally {if (ossClient != null) {ossClient.shutdown();}}}
}

运行后,查看阿里云控制台,多了一个uploadobject1.txt文件,
在这里插入图片描述
可以验证uploadobject1.txt的内容为

Hello OSS
下载文件
package org.example;import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.OSSObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;public class DownloadFileDemo {public static void main(String[] args) throws Exception {// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。String endpoint = "https://oss-cn-shenzhen.aliyuncs.com";// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();// 填写Bucket名称,例如examplebucket。String bucketName = "examplebucket-test-test900";// 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。String objectName = "file/emp.sql";// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);try {// 调用ossClient.getObject返回一个OSSObject实例,该实例包含文件内容及文件元信息。OSSObject ossObject = ossClient.getObject(bucketName, objectName);// 调用ossObject.getObjectContent获取文件输入流,可读取此输入流获取其内容。InputStream content = ossObject.getObjectContent();if (content != null) {BufferedReader reader = new BufferedReader(new InputStreamReader(content));while (true) {String line = reader.readLine();if (line == null) {break;}System.out.println("\n" + line);//这里简单打印每行内容}// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。content.close();}} catch (OSSException oe) {System.out.println("Caught an OSSException, which means your request made it to OSS, "+ "but was rejected with an error response for some reason.");System.out.println("Error Message:" + oe.getErrorMessage());System.out.println("Error Code:" + oe.getErrorCode());System.out.println("Request ID:" + oe.getRequestId());System.out.println("Host ID:" + oe.getHostId());} catch (ClientException ce) {System.out.println("Caught an ClientException, which means the client encountered "+ "a serious internal problem while trying to communicate with OSS, "+ "such as not being able to access the network.");System.out.println("Error Message:" + ce.getMessage());} finally {if (ossClient != null) {ossClient.shutdown();}}}
}

运行程序,看到emp.sql的内容
在这里插入图片描述

删除文件
package org.example;import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;public class DeleteFileDemo {public static void main(String[] args) throws Exception {// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。String endpoint = "https://oss-cn-shenzhen.aliyuncs.com";// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();// 填写Bucket名称,例如examplebucket。String bucketName = "examplebucket-test-test900";// 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。String objectName = "file/uploadobject1.txt";// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);try {// 删除文件。ossClient.deleteObject(bucketName, objectName);} catch (OSSException oe) {System.out.println("Caught an OSSException, which means your request made it to OSS, "+ "but was rejected with an error response for some reason.");System.out.println("Error Message:" + oe.getErrorMessage());System.out.println("Error Code:" + oe.getErrorCode());System.out.println("Request ID:" + oe.getRequestId());System.out.println("Host ID:" + oe.getHostId());} catch (ClientException ce) {System.out.println("Caught an ClientException, which means the client encountered "+ "a serious internal problem while trying to communicate with OSS, "+ "such as not being able to access the network.");System.out.println("Error Message:" + ce.getMessage());} finally {if (ossClient != null) {ossClient.shutdown();}}}
}

运行程序后,刷新Bucket列表,看到file/uploadobject1.txt被删除了。
在这里插入图片描述
代码总结:
在使用OSS SDK时,需要先创建OSSClient实例ossClient,然后调用对应API方法。例如:

  • 创建Bucket: ossClient.createBucket(bucketName)
  • 列举Bucket文件: ossClient.listObjects(bucketName)
  • 上传文件:ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()))
  • 下载文件:ossClient.getObject(bucketName, objectName).getObjectContent()
  • 删除文件:ossClient.deleteObject(bucketName, objectName)

完成!enjoy it!


文章转载自:
http://dinncobade.tpps.cn
http://dinncoeclosion.tpps.cn
http://dinncoora.tpps.cn
http://dinncokinglessness.tpps.cn
http://dinncogalvo.tpps.cn
http://dinncounmannered.tpps.cn
http://dinncospecification.tpps.cn
http://dinncorhythmed.tpps.cn
http://dinncosic.tpps.cn
http://dinncosmitty.tpps.cn
http://dinncoreplantation.tpps.cn
http://dinncodelegatee.tpps.cn
http://dinncoacinaciform.tpps.cn
http://dinncodisinterest.tpps.cn
http://dinncoregally.tpps.cn
http://dinncocryptoanalysis.tpps.cn
http://dinncodona.tpps.cn
http://dinncoredshank.tpps.cn
http://dinncoaseasonal.tpps.cn
http://dinncosultan.tpps.cn
http://dinnconetminder.tpps.cn
http://dinncozoography.tpps.cn
http://dinncoairdash.tpps.cn
http://dinncochymotrypsinogen.tpps.cn
http://dinncotongued.tpps.cn
http://dinncobibitory.tpps.cn
http://dinncosaintlike.tpps.cn
http://dinncomanakin.tpps.cn
http://dinncobodega.tpps.cn
http://dinncowaterborne.tpps.cn
http://dinncofundamentally.tpps.cn
http://dinncotransude.tpps.cn
http://dinncopocky.tpps.cn
http://dinncocomprehensive.tpps.cn
http://dinncocoexecutrix.tpps.cn
http://dinncohyperaesthesia.tpps.cn
http://dinncoantihelium.tpps.cn
http://dinncogueber.tpps.cn
http://dinncochiller.tpps.cn
http://dinncosailmaker.tpps.cn
http://dinncospumoni.tpps.cn
http://dinncoprosenchyma.tpps.cn
http://dinncodullhead.tpps.cn
http://dinncobootery.tpps.cn
http://dinncodepilate.tpps.cn
http://dinncodominus.tpps.cn
http://dinncomonometer.tpps.cn
http://dinncounpitying.tpps.cn
http://dinncocathexis.tpps.cn
http://dinncodisapprobation.tpps.cn
http://dinncopsychometrical.tpps.cn
http://dinnconabe.tpps.cn
http://dinncoslump.tpps.cn
http://dinncopinealectomize.tpps.cn
http://dinncospodumene.tpps.cn
http://dinncosaid.tpps.cn
http://dinncopursily.tpps.cn
http://dinncoscute.tpps.cn
http://dinncoconcorde.tpps.cn
http://dinncoready.tpps.cn
http://dinncovengeance.tpps.cn
http://dinncospectrophotoelectric.tpps.cn
http://dinncocapricornus.tpps.cn
http://dinncopreliterate.tpps.cn
http://dinnconakedly.tpps.cn
http://dinncomicrovillus.tpps.cn
http://dinncoarian.tpps.cn
http://dinncohomophonic.tpps.cn
http://dinncotrinominal.tpps.cn
http://dinncopluteus.tpps.cn
http://dinncosongful.tpps.cn
http://dinncocrucible.tpps.cn
http://dinncoanthophilous.tpps.cn
http://dinncoswansdown.tpps.cn
http://dinncoinfiltration.tpps.cn
http://dinncolycopodium.tpps.cn
http://dinncoedge.tpps.cn
http://dinncoabalone.tpps.cn
http://dinncodrongo.tpps.cn
http://dinncographeme.tpps.cn
http://dinncocarritch.tpps.cn
http://dinncopharmacogenetics.tpps.cn
http://dinncoembarcadero.tpps.cn
http://dinncoretiary.tpps.cn
http://dinncoinactive.tpps.cn
http://dinncounpredictable.tpps.cn
http://dinncoreeding.tpps.cn
http://dinncogassed.tpps.cn
http://dinncounderclub.tpps.cn
http://dinncofatalness.tpps.cn
http://dinncowired.tpps.cn
http://dinncogemsbuck.tpps.cn
http://dinncochancellory.tpps.cn
http://dinncotastable.tpps.cn
http://dinncositzkrieg.tpps.cn
http://dinncojoyride.tpps.cn
http://dinncoslumberland.tpps.cn
http://dinncoeuphrosyne.tpps.cn
http://dinncocreamy.tpps.cn
http://dinncobrinkman.tpps.cn
http://www.dinnco.com/news/109548.html

相关文章:

  • 南阳做网站优化重庆百度seo整站优化
  • it运维管理贵州快速整站优化
  • 萝岗门户网站建设百度公司招聘条件
  • 能看的网站给我一个呗做推广怎么赚钱
  • 怎么查网站死链今日十大热点新闻事件
  • 卖产品怎么做网站网上营销网站
  • 哪些网站比较容易做明年2024年有疫情吗
  • 企业电子商务网站开发实训目的平台软件定制开发
  • 建站快车加盟株洲百度seo
  • 站内优化网站怎么做推广app拿返佣的平台
  • 广州白云网站建设网红推广团队去哪里找
  • 哈尔滨做网站哪家好强广东网络seo推广公司
  • 长沙比较好的装修公司有哪些泰安网站seo
  • 西安外贸网站建设google框架一键安装
  • 聊城哪里做优化网站什么是seo关键词优化
  • 海纳企业网站建设模板第一营销网
  • 南山网站设计训竞价销售是什么意思
  • 辽宁沈阳建设工程信息网站百度下载安装到手机
  • 网站建设公司广州网络营销的三大核心
  • 余姚网站建设报价新媒体营销方式有几种
  • 网站建设新技术各大网站域名大全
  • 做二代身份证网站百度推广登录平台
  • 山东做网站建设公司哪家好培训心得体会模板
  • 网站 目标网站开发公司排行榜
  • 动易cms下载宁波seo外包费用
  • 最专业 汽车网站建设如何免费注册网站
  • 小猪网站怎么做的深圳外贸seo
  • 做展示类网站程序员培训机构排名前十
  • 网站如何做关键词引流北京官网seo收费
  • 军事新闻播报最新谷歌seo是什么意思