aliyun / aliyun-oss-java-sdk

Aliyun OSS SDK for Java
1.22k stars 805 forks source link

项目初始化失败 Can't find bundle for base name oss, locale zh_CN #501

Open lastspring opened 1 year ago

lastspring commented 1 year ago

Caused by: java.lang.ExceptionInInitializerError: null at com.aliyun.oss.OSSClient.toURI(OSSClient.java:311) at com.aliyun.oss.OSSClient.setEndpoint(OSSClient.java:255) at com.aliyun.oss.OSSClient.(OSSClient.java:235) at com.aliyun.oss.OSSClientBuilder.build(OSSClientBuilder.java:39) at com.jftzx.comp.upload.AliOss.(AliOss.java:31) at com.jftzx.comp.upload.AliOssBeanDefinitions.lambda$getAliOssInstanceSupplier$0(AliOssBeanDefinitions.java:17) at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:68) at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:54) at org.springframework.beans.factory.aot.BeanInstanceSupplier.lambda$get$2(BeanInstanceSupplier.java:202) at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:58) at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:46) at org.springframework.beans.factory.aot.BeanInstanceSupplier.invokeBeanSupplier(BeanInstanceSupplier.java:210) at org.springframework.beans.factory.aot.BeanInstanceSupplier.get(BeanInstanceSupplier.java:202) at org.springframework.beans.factory.support.DefaultListableBeanFactory.obtainInstanceFromSupplier(DefaultListableBeanFactory.java:947) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1214) ... 17 common frames omitted Caused by: java.util.MissingResourceException: Can't find bundle for base name oss, locale zh_CN at java.base@17.0.8/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2045) at java.base@17.0.8/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1683) at java.base@17.0.8/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1586) at java.base@17.0.8/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1549) at java.base@17.0.8/java.util.ResourceBundle.getBundle(ResourceBundle.java:932) at com.aliyun.oss.common.utils.ResourceManager.(ResourceManager.java:33) at com.aliyun.oss.common.utils.ResourceManager.getInstance(ResourceManager.java:37) at com.aliyun.oss.internal.OSSUtils.(OSSUtils.java:53) ... 32 common frames omitted

项目经过nativeCompile 构建之后异常

mingchiuli commented 11 months ago

同样的问题,这个sdk没兼容native

lastspring commented 11 months ago

sdk怎么才能兼容native?需要自己手打源码自己适配么?有没有大神开发好了给点经验

lmm1990 commented 8 months ago

遇到了同样的问题

mingchiuli commented 7 months ago

sdk怎么才能兼容native?需要自己手打源码自己适配么?有没有大神开发好了给点经验

可以使用自己调接口的方式上传图片,例如

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.Locale;

@Component
public class OssSignUtils {

    private String accessKeyId;

    private String accessKeySecret;

    private String bucketName;

    private final static String ALGORITHM = "HmacSHA1";

    @SneakyThrows
    private byte[] hmacSha1(String data, String accessKeySecret) {
        Mac mac = Mac.getInstance(ALGORITHM);
        SecretKeySpec keySpec = new SecretKeySpec(accessKeySecret.getBytes(), ALGORITHM);
        mac.init(keySpec);
        return mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
    }

    public String getGMTDate() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.UK);
        LocalDateTime now = LocalDateTime.now(ZoneId.of("GMT"));
        return dateTimeFormatter.format(now);
    }

    private String buildSignData(String date, String canonicalizedResource, String methodName, String contentType) {
        //https://help.aliyun.com/zh/oss/developer-reference/include-signatures-in-the-authorization-header?spm=a2c4g.11186623.0.0.54e828efd3PoE6
        return  methodName + "\n" +
                "\n" +
                contentType + "\n" +
                date + "\n" +
                canonicalizedResource;
    }

    public String getAuthorization(String objectName, String method, String contentType) {
        String date = getGMTDate();
        String signData = buildSignData(date, "/" + bucketName + "/" + objectName, method, contentType);
        byte[] bytes = hmacSha1(signData, accessKeySecret);
        String signature = Base64.getEncoder().encodeToString(bytes);
        return  "OSS " + accessKeyId + ":" + signature;
    }
}

使用时:

public void deleteOss(String url) {
        String objectName = url.replace(baseUrl + "/", "");
        Map<String, String> headers = new HashMap<>();
        String gmtDate = ossSignUtils.getGMTDate();
        headers.put(HttpHeaders.DATE, gmtDate);
        headers.put(HttpHeaders.AUTHORIZATION, ossSignUtils.getAuthorization(objectName, HttpMethod.DELETE.name(), ""));
        ossHttpService.deleteOssObject(objectName, headers);
    }