文件路径转换

huangapple go评论65阅读模式
英文:

String to FileInputSteam

问题

FileInputStream serviceAccount =
    new FileInputStream("push-manager-app/src/main/resources/serviceAccountKey.json");
FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .setDatabaseUrl("https://test1-use.firebaseio.com")
                .build();
FirebaseApp.initializeApp(options);

上述的 Firebase 连接器代码工作正常。但在 openshift 环境中,我不能将 serviceAccountKey.json 提交到资源中,因为它在不同的环境中会有所不同。我可以从每个环境的 openshift 配置映射中获取它作为 String

为了运行这段代码,我需要将 String 转换为 FileInputStream。我不确定如何做。我有一个变通方法,可以读取 String,生成文件并使用它。但我希望使用正确的方法。我检查了 Firebase API 的其他选项,但它甚至无法处理 InputStream


<details>
<summary>英文:</summary>

FileInputStream serviceAccount =
new FileInputStream("push-manager-app/src/main/resources/serviceAccountKey.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://test1-use.firebaseio.com")
.build();
FirebaseApp.initializeApp(options);


The above code for `Firebase` connector is working fine. But in `openshift` environment I can&#39;t commit `serviceAccountKey.json` in resources as it will be different for different environments. I can get it as `String` from `openshift` configmap in each environment.

For running this block of code I need to convert `String` in `FileInputStream`. I&#39;m not sure how to do it. I have one workaround where I can read `String`, generate file and use it. But I wanted to use the right way. I checked `Firebase` API for other options but it can&#39;t even handle `InputStream`.


</details>


# 答案1
**得分**: 1

String json = "xxxxxx";
InputStream stream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));


<details>
<summary>英文:</summary>

String json = "xxxxxx";
InputStream stream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));


</details>



# 答案2
**得分**: 0

```java
private static InputStream getFile() {
    return FireBaseService.class.getResourceAsStream("/dataCred.json");
}
private static final String DATABASE_URL = "DATABASE_URL";

public static void initiainzeSDK() {
    try {
        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(getFile()))
                .setDatabaseUrl(DATABASE_URL)
                .build();

        FirebaseApp.initializeApp(options);
        // Initialize the default app

    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

make sure ur json file not wrong syntax
英文:
private static InputStream getFile() {
    return FireBaseService.class.getResourceAsStream(&quot;/dataCred.json&quot;);
}
private static final String DATABASE_URL = &quot;DATABASE_URL&quot;;

public static void initiainzeSDK() {
    
   try {
        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(getFile()))
                .setDatabaseUrl(DATABASE_URL)
                .build();

    FirebaseApp.initializeApp(options);
    // Initialize the default app

    } catch (IOException e) {
       System.out.println(e.getMessage());
   }
}

make sure ur json file not wrong syntax

huangapple
  • 本文由 发表于 2020年7月23日 15:26:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63049007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定