>> Hey, Josh. We're back. Now, we're going to continue our series
of Spring talking to Azure. What are we hitting up today?
>> Well, I think we should look at
the Object Storage Service which is
a way to upload things to Microsoft Azure.
>> Yes. If you're developing
a Spring web app or Spring in other apps,
one of the common patterns and scenarios that people have to
do is let me throw my photos up to you,
or I've got files that are coming from other places,
so let's go shove them somewhere.
>> Yeah. While there is
no particular Spring Integration here, it is so easy.
I mean, there's configuration, but it is basically
so easy that the API is just a very quick demo,
and I figured we should definitely show that.
Because it's one of those very useful things
that Spring developers will want to do.
Just like you said, there's always a use case for
storing blobs of binary data somewhere.
>> Let's see how quick this goes.
>> First things first, go to your "Storage Accounts".
I've got a storage account
here called "Bootifulstorage", I'll go to that.
There, I have my Containers.
Now, Containers are the buckets into which you can put data.
I have a bucket called Cats.
There's nothing in there right now,
so I want to fix that by
uploading some cats, it's the Internet after all.
We're going to use Object Storage, so Azure-OSS.
We're going to use Java 11 because
that's a nice [inaudible] version for Azure Spring Cloud.
Then I want to use the Azure Support,
and I'm going to use 2.2.8,
I think Spring 2.2.8 just in case.
That's it. That's all I want to use is I want to use
these different dependencies in Azure Storage,
and the web support.
Do I want web support? I don't think I do.
I think we're just going to upload it straight
to Azure when the application starts up.
So I'm satisfied with those selections.
Let's hit "Generate", that'll give us a new project.
We can open this up in our IDE.
Now, first things first,
when the dependencies have downloaded and all that,
we're going to need to specify some configuration that tells
the Azure Integration about which storage accounts we want to use.
I'm going to just use this configuration that I've already got,
Azure Storage account name equals Bootifulstorage,
the container name equals Cats,
and then the Azure Object Storage account key
is in an environment variable on my local machine,
and you could have gotten that by going to
your Azure portal and finding the appropriate value there.
Once you've got that,
then it's a trivial matter to write code that
actually writes data to the platform, to the Cloud.
What we're going to do is we're going to inject
a reference to a cat picture and then use that to upload.
So I'm going to create a class here,
class ObjectStorageDemo or ObjectStorageServiceDemo.
That's even better. We're just
going to create a simple class here,
just a thing that's going to have a constructor.
In the constructor, we're going to use
Springs Integration for resources.
Now, resource in Spring is anything that can produce bytes.
So we have an abstraction,
and you can see this abstraction gives
you byte array resources, classpath resources,
contexts resources, file system resources,
file URL resources, InputStream resources,
multi-part resources, path resources,
vfs is a virtual file system,
and then many others besides.
So if you're using MongoDB,
we have GridFS resources,
if you're using some of those other Cloud providers,
there's also things like that,
so lots of different resources there.
It's an abstraction. It's just very simple.
What I'm going to do is I'm going to tell Spring to give me
a reference to one that I've got laying around in the file system.
I'm going to read the input for this from the file system.
So it'll be on the classpath,
and it's going to be called cat.jpg.
I'm going to want to put that resulting binary data,
I'm going to want to take that stream
that I get from that resource,
and then upload it to the platform.
I'm going to do so using a container URL.
This is part of the Azure API.
Very cool, very simple API.
The only thing I need to do now is I need to make
sure that I had that cat.jpg.
So I'm going to go to my terminal here.
Terminal, copy from the desktop
cat.jpg to the source main resources cat.jpg directory.
The cat.jpg is a blob cat,
it's a binary blob of cat.
It's a cat blob. I figured it would be perfect for our demo.
We've got a container URL,
and we're just going to use
that container URL to create a block blob URL,
so containerURL.createBlockBlobURL cat.jpg,
and then we're going to upload the file.
Now, the file, we need
the bytes from the input stream, the resource.
So we're going to say resource.getfile,
it'll give me a Java.io file reference.
Then from there, I want to take
the bytes from that file and copy it into a byte array,
so copy to byte array, so file.
That I think is most of what I need.
But I also need to throw an exception because that
is our io-sensitive operations,
so they might fail.
I've got now an array of bytes,
and then from there, I want to upload them.
So I'm going to say
containerURL createBlockBlobURL upload, flowable.
So I need to create a reactive type here,.just ByteBuffer.wrap.
I'm going to wrap the bytes.
How many bytes am I going to send? I'm going to send all of them.
So bytes.length and then null,
null, null, and null.
Then finally, I need to do a blockingGet.
So what is the issue here?
Well, it seems I've missed something. Oh, yes.
We've got all of that in the wrong parentheses, and there we go.
Voila. So the result of that is a block blob upload response,
and then we can just log out the result.
So blockBlobUploadResponse.toString result,
system out result.
Let's just see. I've got nothing up my sleeve.
So if I go here,
do a "Refresh", now let's go ahead and run the application.
>> The only difference you've made here,
if you were in production and looking at the bigger one,
you might go grab that big file in chunks in [inaudible]?
>> Yeah, exactly. You actually can and should.
Here is the file,
it's there in the container.
There's the block blob cat.
>> That's a great picture.
>> It is a great picture. Cat pictures,
powering the Internet since the Internet.