OSS base64图片上传
base64格式:data:image/png;base64, 注意','符号。
public static void main(String[] args) {
// base64文件
String base64ByCode = Objects.requireNonNull(getHeadPhotoBase64ByCode('1')).replace(base64_pre, '');
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
String objectName = 'images/2.png';
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
// 填写Byte数组。
byte[] content = new BASE64Decoder().decodeBuffer(base64ByCode);
// 创建PutObject请求。
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content));
} 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());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
赞0
踩0