|
41 | 41 | import androidx.core.content.FileProvider;
|
42 | 42 | import androidx.test.ext.junit.runners.AndroidJUnit4;
|
43 | 43 |
|
| 44 | +import com.tencent.cos.xml.CosXmlService; |
44 | 45 | import com.tencent.cos.xml.CosXmlServiceConfig;
|
45 | 46 | import com.tencent.cos.xml.CosXmlSimpleService;
|
46 | 47 | import com.tencent.cos.xml.core.MyOnSignatureListener;
|
|
65 | 66 | import com.tencent.cos.xml.utils.DigestUtils;
|
66 | 67 | import com.tencent.cos.xml.utils.UrlUtil;
|
67 | 68 | import com.tencent.qcloud.core.auth.COSXmlSignSourceProvider;
|
| 69 | +import com.tencent.qcloud.core.auth.SessionQCloudCredentials; |
68 | 70 | import com.tencent.qcloud.core.auth.ShortTimeCredentialProvider;
|
69 | 71 | import com.tencent.qcloud.core.http.HttpTaskMetrics;
|
70 | 72 | import com.tencent.qcloud.core.logger.QCloudLogger;
|
71 | 73 | import com.tencent.qcloud.core.util.Base64Utils;
|
72 | 74 | import com.tencent.qcloud.core.util.QCloudStringUtils;
|
73 | 75 |
|
| 76 | +import org.json.JSONException; |
| 77 | +import org.json.JSONObject; |
74 | 78 | import org.junit.Assert;
|
75 | 79 | import org.junit.Rule;
|
76 | 80 | import org.junit.Test;
|
77 | 81 | import org.junit.rules.ErrorCollector;
|
78 | 82 | import org.junit.runner.RunWith;
|
79 | 83 |
|
| 84 | +import java.io.BufferedReader; |
80 | 85 | import java.io.File;
|
81 | 86 | import java.io.IOException;
|
82 | 87 | import java.io.InputStream;
|
| 88 | +import java.io.InputStreamReader; |
| 89 | +import java.net.HttpURLConnection; |
83 | 90 | import java.net.MalformedURLException;
|
84 | 91 | import java.net.URL;
|
85 | 92 | import java.nio.file.Files;
|
@@ -1879,4 +1886,117 @@ public void run() {
|
1879 | 1886 | uploadLocker.lock();
|
1880 | 1887 | TestUtils.assertCOSXMLTaskSuccess(cosxmlUploadTask);
|
1881 | 1888 | }
|
| 1889 | + |
| 1890 | + public void testKeyAndCredentialsUpload() throws JSONException { |
| 1891 | + // 假设需要上传的文件本地路径为filePath |
| 1892 | + String filePath = "/path/to/your/file.txt"; |
| 1893 | + |
| 1894 | +// 1、从服务端请求上传和签名信息 |
| 1895 | + File file = new File(filePath); |
| 1896 | +// getKeyAndCredentials方法见下一个代码块 |
| 1897 | + JSONObject keyAndCredentials = getKeyAndCredentials(file.getName()); |
| 1898 | + String region = keyAndCredentials.getString("region"); |
| 1899 | + String bucket = keyAndCredentials.getString("bucket"); |
| 1900 | + String cosKey = keyAndCredentials.getString("key"); |
| 1901 | + long startTime = keyAndCredentials.getLong("startTime"); |
| 1902 | + long expiredTime = keyAndCredentials.getLong("expiredTime"); |
| 1903 | + JSONObject credentials = keyAndCredentials.getJSONObject("credentials"); |
| 1904 | + String tmpSecretId = credentials.getString("tmpSecretId"); |
| 1905 | + String tmpSecretKey = credentials.getString("tmpSecretKey"); |
| 1906 | + String sessionToken = credentials.getString("sessionToken"); |
| 1907 | + |
| 1908 | +// 2、初始化 COS SDK: CosXmlService和TransferManager |
| 1909 | +// 创建 CosXmlServiceConfig 对象,根据需要修改默认的配置参数 |
| 1910 | + CosXmlServiceConfig serviceConfig = new CosXmlServiceConfig.Builder() |
| 1911 | + .setRegion(region) |
| 1912 | + .isHttps(true) // 使用 HTTPS 请求, 默认为 HTTP 请求 |
| 1913 | + .builder(); |
| 1914 | +// 初始化一个 CosXmlService 的实例,可以不设置临时密钥回调 |
| 1915 | + CosXmlService cosXmlService = new CosXmlService(getContext(), serviceConfig); |
| 1916 | +// 初始化 TransferConfig,这里使用默认配置,如果需要定制,请参考 SDK 接口文档 |
| 1917 | + TransferConfig transferConfig = new TransferConfig.Builder().build(); |
| 1918 | +// 初始化 TransferManager |
| 1919 | + TransferManager transferManager = new TransferManager(cosXmlService, transferConfig); |
| 1920 | + |
| 1921 | +// 3、进行上传 |
| 1922 | + PutObjectRequest putRequest = new PutObjectRequest(bucket, cosKey, filePath); |
| 1923 | + SessionQCloudCredentials sessionQCloudCredentials = new SessionQCloudCredentials(tmpSecretId, tmpSecretKey, |
| 1924 | + sessionToken, startTime, expiredTime); |
| 1925 | + putRequest.setCredential(sessionQCloudCredentials); |
| 1926 | + COSXMLUploadTask uploadTask = transferManager.upload(putRequest, null); |
| 1927 | +//设置上传进度回调 |
| 1928 | + uploadTask.setCosXmlProgressListener(new CosXmlProgressListener() { |
| 1929 | + @Override |
| 1930 | + public void onProgress(long complete, long target) { |
| 1931 | + // todo Do something to update progress... |
| 1932 | + } |
| 1933 | + }); |
| 1934 | +//设置返回结果回调 |
| 1935 | + uploadTask.setCosXmlResultListener(new CosXmlResultListener() { |
| 1936 | + @Override |
| 1937 | + public void onSuccess(CosXmlRequest request, CosXmlResult result) { |
| 1938 | + COSXMLUploadTask.COSXMLUploadTaskResult uploadResult = |
| 1939 | + (COSXMLUploadTask.COSXMLUploadTaskResult) result; |
| 1940 | + } |
| 1941 | + |
| 1942 | + // 如果您使用 kotlin 语言来调用,请注意回调方法中的异常是可空的,否则不会回调 onFail 方法,即: |
| 1943 | + // clientException 的类型为 CosXmlClientException?,serviceException 的类型为 CosXmlServiceException? |
| 1944 | + @Override |
| 1945 | + public void onFail(CosXmlRequest request, |
| 1946 | + @Nullable CosXmlClientException clientException, |
| 1947 | + @Nullable CosXmlServiceException serviceException) { |
| 1948 | + if (clientException != null) { |
| 1949 | + clientException.printStackTrace(); |
| 1950 | + } else { |
| 1951 | + serviceException.printStackTrace(); |
| 1952 | + } |
| 1953 | + } |
| 1954 | + }); |
| 1955 | + } |
| 1956 | + |
| 1957 | + /** |
| 1958 | + * 获取上传和签名信息 |
| 1959 | + * |
| 1960 | + * @param filename 文件名 |
| 1961 | + * @return 上传和签名信息 |
| 1962 | + */ |
| 1963 | + private JSONObject getKeyAndCredentials(String filename) { |
| 1964 | + // 获取上传和签名信息 |
| 1965 | + HttpURLConnection getConnection = null; |
| 1966 | + try { |
| 1967 | + //上面搭建的临时密钥服务(正式环境 请替换成正式的业务url) |
| 1968 | + URL url = new URL("http://X.X.X.X:3000/getKeyAndCredentials?filename=" + filename); |
| 1969 | + getConnection = (HttpURLConnection) url.openConnection(); |
| 1970 | + getConnection.setRequestMethod("GET"); |
| 1971 | + |
| 1972 | + int responseCode = getConnection.getResponseCode(); |
| 1973 | + if (responseCode == HttpURLConnection.HTTP_OK) { |
| 1974 | + BufferedReader reader = new BufferedReader(new InputStreamReader(getConnection.getInputStream())); |
| 1975 | + StringBuilder stringBuilder = new StringBuilder(); |
| 1976 | + String line; |
| 1977 | + while ((line = reader.readLine()) != null) { |
| 1978 | + stringBuilder.append(line); |
| 1979 | + } |
| 1980 | + reader.close(); |
| 1981 | + JSONObject jsonObject; |
| 1982 | + try { |
| 1983 | + // 服务端接口需要返回:上传的存储桶、地域、随机路径的对象键、临时密钥 |
| 1984 | + jsonObject = new JSONObject(stringBuilder.toString()); |
| 1985 | + return jsonObject; |
| 1986 | + } catch (JSONException e) { |
| 1987 | + e.printStackTrace(); |
| 1988 | + } |
| 1989 | + } else { |
| 1990 | + Log.e("getKeyAndCredentials", "getKeyAndCredentials HTTP error code: " + responseCode); |
| 1991 | + } |
| 1992 | + } catch (IOException e) { |
| 1993 | + e.printStackTrace(); |
| 1994 | + Log.e("getKeyAndCredentials", "getKeyAndCredentials Error sending GET request: " + e.getMessage()); |
| 1995 | + } finally { |
| 1996 | + if (getConnection != null) { |
| 1997 | + getConnection.disconnect(); |
| 1998 | + } |
| 1999 | + } |
| 2000 | + return null; |
| 2001 | + } |
1882 | 2002 | }
|
0 commit comments