Skip to content

Instantly share code, notes, and snippets.

@ulve
Last active May 2, 2020 20:35
Show Gist options
  • Save ulve/b5378587f1deb20029e3556aa6aa2540 to your computer and use it in GitHub Desktop.
Save ulve/b5378587f1deb20029e3556aa6aa2540 to your computer and use it in GitHub Desktop.
;; container - An existing container at the sotrage account
;; blob-connection-string - Connectionstring for the strage account
;; This is used from environ to have different configurations
{:dev {:env {:container "<containerName>"
:blob-connection-string "DefaultEndpointsProtocol=https;AccountName=<name>;AccountKey=<key>;EndpointSuffix=core.windows.net"}}}
(defproject fist "0.1.0-SNAPSHOT"
:description "Example of how to upload a blob to azure blob storage"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:plugins [[lein-environ "1.1.0"]]
:dependencies [[org.clojure/clojure "1.10.1"]
[com.azure/azure-storage-blob "12.6.0"]
[environ "1.1.0"]]
:repl-options {:init-ns fist.core}
:main fist.core)
(ns fist.core
(:import (com.azure.storage.blob BlobServiceClientBuilder))
(:require [environ.core :as environ]))
(defn get-storage-config
"Gets the connectionstring and container name from the configuration.
If you dont want to use environ just use a map containing connection-string and container"
[]
(let [blob-connection-string (environ/env :blob-connection-string)
container (environ/env :container)]
{:connection-string blob-connection-string
:container container}))
(defn create-blob-service-client
"Creates a blob service client using the connection string"
[{connection-string :connection-string}]
(let [builder (new BlobServiceClientBuilder)
service-client (.buildClient (.connectionString builder connection-string))]
service-client))
(defn create-container-client
"Creates a container client to interact with containers"
[client {container :container}]
(.getBlobContainerClient client container))
(defn upload-file
"Uploads a file"
[client file-name]
(.uploadFromFile (.getBlobClient client file-name) file-name))
(defn -main
"Will upload a file to azure blob storage."
[]
(let [config (get-storage-config)
service-client (create-blob-service-client config)
container-client (create-container-client service-client config)]
(upload-file container-client "./test.txt")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment