Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lastspring opened this issue Nov 5, 2023 · 4 comments

Comments

@lastspring
Copy link

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.AliOss__BeanDefinitions.lambda$getAliOssInstanceSupplier$0(AliOss__BeanDefinitions.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 [email protected]/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2045)
at [email protected]/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1683)
at [email protected]/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1586)
at [email protected]/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1549)
at [email protected]/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
Copy link

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

@lastspring
Copy link
Author

lastspring commented Jan 5, 2024

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

@lmm1990
Copy link

lmm1990 commented Mar 12, 2024

遇到了同样的问题

@mingchiuli
Copy link

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);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants