3333
3434@ Slf4j
3535public final class OSSUtils {
36+ /**
37+ * Upload file to OSS without certificate reuse.
38+ *
39+ * @param model Model name
40+ * @param filePath Local file path
41+ * @param apiKey API key
42+ * @return OSS URL
43+ * @throws NoApiKeyException If API key is missing
44+ */
3645 public static String upload (String model , String filePath , String apiKey )
3746 throws NoApiKeyException {
47+ UploadResult result = uploadWithCertificate (model , filePath , apiKey ,
48+ null );
49+ return result .getOssUrl ();
50+ }
51+
52+ /**
53+ * Upload file to OSS with optional certificate reuse.
54+ *
55+ * @param model Model name
56+ * @param filePath Local file path
57+ * @param apiKey API key
58+ * @param certificate Optional upload certificate for reuse
59+ * @return UploadResult containing OSS URL and certificate
60+ * @throws NoApiKeyException If API key is missing
61+ */
62+ public static UploadResult uploadWithCertificate (String model ,
63+ String filePath , String apiKey , OSSUploadCertificate certificate )
64+ throws NoApiKeyException {
3865 OkHttpClient client = OkHttpClientFactory .getOkHttpClient ();
39- DashScopeResult uploadInfo = get_upload_certificate (model , apiKey );
40- JsonObject outputData = ((JsonObject ) uploadInfo .getOutput ()).getAsJsonObject ("data" );
66+ OSSUploadCertificate cert = certificate ;
67+
68+ // Get certificate if not provided
69+ if (cert == null ) {
70+ DashScopeResult uploadInfo = get_upload_certificate (model , apiKey );
71+ JsonObject outputData = ((JsonObject ) uploadInfo .getOutput ())
72+ .getAsJsonObject ("data" );
73+ cert = new OSSUploadCertificate (
74+ outputData .get ("upload_host" ).getAsString (),
75+ outputData .get ("oss_access_key_id" ).getAsString (),
76+ outputData .get ("signature" ).getAsString (),
77+ outputData .get ("policy" ).getAsString (),
78+ outputData .get ("upload_dir" ).getAsString (),
79+ outputData .get ("x_oss_object_acl" ).getAsString (),
80+ outputData .get ("x_oss_forbid_overwrite" ).getAsString ()
81+ );
82+ }
83+
4184 Map <String , String > headers = new HashMap <>();
4285 headers .put ("user-agent" , DashScopeHeaders .userAgent ());
4386 headers .put ("Accept" , "application/json" );
4487 File uploadFile = new File (filePath );
45- String host = outputData . get ( "upload_host" ). getAsString ();
46- String ossAccessKeyId = outputData . get ( "oss_access_key_id" ). getAsString ();
47- String signature = outputData . get ( "signature" ). getAsString ();
48- String policy = outputData . get ( "policy" ). getAsString ();
49- String key = outputData . get ( "upload_dir" ). getAsString () + "/" + uploadFile .getName ();
50- String xOssObjectAcl = outputData . get ( "x_oss_object_acl" ). getAsString ();
51- String xOssForbidOverwrite = outputData . get ( "x_oss_forbid_overwrite" ). getAsString ();
88+ String host = cert . getUploadHost ();
89+ String ossAccessKeyId = cert . getOssAccessKeyId ();
90+ String signature = cert . getSignature ();
91+ String policy = cert . getPolicy ();
92+ String key = cert . getUploadDir () + "/" + uploadFile .getName ();
93+ String xOssObjectAcl = cert . getXOssObjectAcl ();
94+ String xOssForbidOverwrite = cert . getXOssForbidOverwrite ();
5295
5396 RequestBody requestBody =
5497 new MultipartBody .Builder ()
@@ -67,13 +110,15 @@ public static String upload(String model, String filePath, String apiKey)
67110 RequestBody .create (MediaType .parse (getContentType (filePath )), uploadFile ))
68111 .build ();
69112
70- Request request = new Request .Builder ().url (host ).post (requestBody ).build ();
113+ Request request = new Request .Builder ().url (host ).post (requestBody )
114+ .build ();
71115 try (Response response = client .newCall (request ).execute ()) {
72116 if (!response .isSuccessful ()) {
73117 Status status = parseFailed (response );
74118 throw new ApiException (status );
75119 }
76- return String .format ("oss://%s" , key );
120+ String ossUrl = String .format ("oss://%s" , key );
121+ return new UploadResult (ossUrl , cert );
77122 } catch (Throwable e ) {
78123 throw new ApiException (e );
79124 }
0 commit comments