In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how Android R uploads pictures to OSS. The quality of the article is high, so Xiaobian shares it with you for reference. I hope you have a certain understanding of relevant knowledge after reading this article.
First initialize the oss object
// ACCESS_ID,ACCESS_KEY is applied in [Aliyun](https://l.gushuji.site/aliyun) OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(ACCESS_ID, ACCESS_KEY); ClientConfiguration conf = new ClientConfiguration(); conf.setConnectionTimeout(15 * 1000); //Connection timeout, default 15 seconds conf.setSocketTimeout(15 * 1000); // socket timeout, default 15 seconds conf.setMaxConcurrentRequest(8); //Maximum number of concurrent requests, default 5 conf.setMaxErrorRetry(2); //Maximum number of retries after failure, default 2 // oss is a global variable, OSS_ENDPOINT is an OSS area address oss = new OSSClient(getApplicationContext(), OSS_ENDPOINT, credentialProvider, conf);
OSS upload pictures are divided into synchronous and asynchronous
synchronization method
//construct upload request PutObjectRequest put = new PutObjectRequest("", "", "");//file meta information setting is optional// ObjectMetadata metadata = new ObjectMetadata();// metadata.setContentType("application/octet-stream"); //set content-type// metadata.setContentMD5(BinaryUtil.calculateBase64Md5(uploadFilePath)); //verify MD5// put.setMetadata(metadata);try { PutObjectResult putResult = oss.putObject(put); Log.d("PutObject", "UploadSuccess"); Log.d("ETag", putResult.getETag()); Log.d("RequestId", putResult.getRequestId());} catch (ClientException e) { //Local anomalies such as network anomalies e.printStackTrace();} catch (ServiceException e) { //Service exception Log.e("RequestId", e.getRequestId()); Log.e("ErrorCode", e.getErrorCode()); Log.e("HostId", e.getHostId()); Log.e("RawMessage", e.getRawMessage());}
Here, this bucketName is configured on Alibaba Cloud platform to store files, and private and public attributes can be set.
objectKey is the name of the file used to query images from the platform
FilePath is the uri path of the selected file
asynchronous upload method
//construct upload request PutObjectRequest put = new PutObjectRequest("", "", "");//during asynchronous upload, you can set progress callback put.setProgressCallback(new OSSProgressCallback() { @Override public void onProgress(PutObjectRequest request, long currentSize, long totalSize) { Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize); }});OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback() { @Override public void onSuccess(PutObjectRequest request, PutObjectResult result) { Log.d("PutObject", "UploadSuccess"); Log.d("ETag", result.getETag()); Log.d("RequestId", result.getRequestId()); } @Override public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) { //request exception if (clientExcepion != null) { //Local anomalies such as network anomalies clientExcepion.printStackTrace(); } if (serviceException != null) { //Service exception Log.e("ErrorCode", serviceException.getErrorCode()); Log.e("RequestId", serviceException.getRequestId()); Log.e("HostId", serviceException.getHostId()); Log.e("RawMessage", serviceException.getRawMessage()); } }});// task.cancel(); //can cancel the task// task.waitUntilFinished(); //can wait for the task to complete
Generally, when uploading successfully, the client needs to pass the objectkey of the file to the background, and then return the url of the image to us through splicing
About Android R how to upload pictures to OSS to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.