com.google.cloud.storage.StorageImpl 的 clojure

尝试与https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud进行一些 java 互操作/storage/StorageImpl.java#L139 -create带有字节数组的方法。


我的repl中有:


user=> (->> s r/reflect :members 

               (filter #(instance? clojure.reflect.Method %)) 

               (filter #(:public (:flags %))) 

               (filter #(= (str (:name %)) "create")) 

               (print-table [:name :flags :parameter-types]))


|  :name |              :flags |                                                                                           :parameter-types |

|--------+---------------------+------------------------------------------------------------------------------------------------------------|

| create | #{:varargs :public} |             [com.google.cloud.storage.BlobInfo byte<> com.google.cloud.storage.Storage$BlobTargetOption<>] |

(还有其他的,但这似乎是最相关的。)


还:


user=> s

#object[com.google.cloud.storage.StorageImpl 0x57fb59c8 "com.google.cloud.storage.StorageImpl@57fb59c8"]

user=> blob-info

#object[com.google.cloud.storage.BlobInfo$BuilderImpl 0x1e8ce729 "com.google.cloud.storage.BlobInfo$BuilderImpl@1e8ce729"]

user=> b

#whidbey/bin "SEVZIE1ZIEdVWQ==“

但是当我打电话时.create,我得到:


user=> (.create s blob-info (bytes b))


java.lang.IllegalArgumentException: No matching method create found taking 2 args for class com.google.cloud.storage.StorageImpl

如果我尝试添加nil为第三个参数,我会得到与3 args.


我在这里遗漏了一些明显的东西吗?谢谢!


编辑:如何在 clojure 中处理 java 可变长度参数?非常相似,而且更通用(这很好)。这最终成为关于一个特定create功能签名的特定问题。


月关宝盒
浏览 85回答 2
2回答

慕容3067478

答案最终是(来自 clojurians slack 上的 seancorfield)这blob-info是一个BuilderImpl内部阶级,需要是一个实际的BlobInfo. 有效的代码:(defn get-storage []&nbsp; (-> (StorageOptions/getDefaultInstance)&nbsp; &nbsp; &nbsp; (.getService)))(defn get-blob-info [bucket storage-key]&nbsp; (let [content-type "text/plain"&nbsp; &nbsp; &nbsp; &nbsp; blob-id (BlobId/of bucket storage-key)&nbsp; &nbsp; &nbsp; &nbsp; builder (doto&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (BlobInfo/newBuilder blob-id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (.setContentType content-type))]&nbsp; &nbsp; (.build builder)))(defn upload-str [bucket storage-key str-to-store]&nbsp; (let [storage (get-storage)&nbsp; &nbsp; &nbsp; &nbsp; blob-info (get-blob-info bucket storage-key)&nbsp; &nbsp; &nbsp; &nbsp; byte-arr (.getBytes str-to-store)]&nbsp; &nbsp; (.create storage&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blob-info&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;byte-arr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(into-array Storage$BlobTargetOption []))))不需要类型提示 - 只需要正确排列类型。

猛跑小猪

我不确定(bytes b)语法是否正确(什么是#whidbey/bin???)。也许试试(byte-array [1 2 3])&nbsp;或类似的。您也可以尝试对参数进行类型提示:(.create s&nbsp;&nbsp; &nbsp; blob-info&nbsp; &nbsp; ^"[B" (byte-array [1 2 3])&nbsp; &nbsp; ; type-hinted param)更新这是我认为您需要的类型提示的示例:(let [byte-array-obj&nbsp; (byte-array [1 2 3])&nbsp; &nbsp; &nbsp; sss&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(java.util.Arrays/toString&nbsp; ^"[B"&nbsp; byte-array-obj) ]&nbsp; &nbsp; (spyxx byte-array-obj)&nbsp; &nbsp; (spyx (type byte-array-obj))&nbsp; &nbsp; (spyx (aget byte-array-obj 2))&nbsp; &nbsp; (spyx sss))结果:byte-array-obj => <#[B #object["[B" 0x26071f95 "[B@26071f95"]>(type byte-array-obj) => [B(aget byte-array-obj 2) => 3sss => "[1, 2, 3]"请注意,Clojure 有一种简单的方法来键入提示,而无需求助于 java-native"[B"字符串语法:(java.util.Arrays/toString ^"[B"&nbsp; byte-array-obj)&nbsp; ; type hint using a string(java.util.Arrays/toString ^bytes byte-array-obj)&nbsp; ; Clojure "build-in" type hint&nbsp;两者是等价的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java