简体   繁体   English

如何为 gs 注册 URL stream 处理程序:Java Z38008ECDD81C2CEAFF1D71 中的协议

[英]How to register a URL stream handler for the gs: protocol in Java Spring

I have code that must be able to handle "normal" urls like "file:///..." and also "gs://..." files.我的代码必须能够处理“file:///...”之类的“普通”url,以及“gs://...”文件。

I have an application.yaml file that has我有一个 application.yaml 文件

bannedKeywordsFile: gs://myproject.appspot.com/mybucket/myfile.csv

This fails with a MalformedURLException.这失败并出现 MalformedURLException。

@Value("${bannedKeywordsFile}")
private Resource bannedKeywordsFile;

private URL myUrl() {
    return bannedKeywordsFile.getURL();
}

To be fair, the Google docs explain this is expected unless a protocol handler is registered.公平地说,除非注册了协议处理程序,否则谷歌文档解释这是预期的。 Is there a handler that can be registered?有可以注册的处理程序吗? Or must one be written from scratch?还是必须从头开始编写?

gs is a scheme that is used for identifying resources stored in Google Cloud Storage Java URL will throw an exception if the protocol is not valid. gs是一种用于标识存储在Google Cloud StorageJavaprotocol Then you got that exception, how you might to use a valid URL you might change然后你得到了那个例外,你如何使用有效的URL你可能会改变

 gs://myproject.appspot.com/mybucket/myfile.csv

to

 https://storage.googleapis.com/my-prject-com/mybucket/myfile.svc

then later validate with Java URL if this is a valid URL.然后稍后使用Java URL进行验证,如果这是一个有效的 URL。

UPDATED更新

Based what you want to do, then you need to create a custom validator so that this validator can be added different schemes, you might use UrlValidator of Apache Common library.根据您想要做的事情,您需要创建一个custom validator ,以便可以添加不同的方案,您可以使用UrlValidator通用库的 UrlValidator。 Then you can add custom schmes by following way:然后,您可以通过以下方式添加custom schmes

import org.apache.commons.validator.routines.UrlValidator;


    String[] customSchemes = { "gs", "file" };
    UrlValidator customValidator = new UrlValidator(customSchemes);
    customValidator.isValid("gs://www.somwebsite.org");// true
    customValidator.isValid("file:///home/user/Desktop/fileName.png");  // true

JIC, this dependency should be added, if you are using maven : JIC,如果您使用的是maven ,则应添加此依赖项:

    <!-- https://mvnrepository.com/artifact/commons-validator/commons-validator -->
    <dependency>
        <groupId>commons-validator</groupId>
        <artifactId>commons-validator</artifactId>
        <version>1.6</version>
    </dependency>

Or If not, then for gradle或者如果不是,那么对于gradle

    // https://mvnrepository.com/artifact/commons-validator/commons-validator
    compile group: 'commons-validator', name: 'commons-validator', version: '1.6'

One possibility is to ignore the Google GCS "url" prefix and access the files using HTTP.一种可能性是忽略 Google GCS“url”前缀并使用 HTTP 访问文件。 For example:例如:

gs://bucket/file

can be accessed as:可以访问为:

http://storage.googleapis.com/bucket/file

See:看:

How do you get or generate a URL to the object in a bucket? 如何获取或生成 URL 到存储桶中的 object?

This basically side-steps the original notion of thinking of gs://... as anything special.这基本上回避了将gs://...视为特殊事物原始概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM