I'm Fumiya Kume, a second year student at Daido University. I'm playing with Xamarin.
Azure Blob Storage offers exabytes of capacity and extremely high scalability, making it easy and cost-effective to place hundreds to billions of objects in the hot or cool tier, depending on how often you need to access your data. Can be stored. You can store all kinds of unstructured data, including images, videos, audio and documents.
Quoted from https://azure.microsoft.com/ja-jp/services/storage/blobs/
It's accurate, but it's difficult to read, so I'll break it down a bit. Simply put, it's a storage that can store any file, whether it's an image, text, or video.
If you use it to save data posted by users on SNS etc., it will be easier to understand if you can imagine it.
The cost of saving files and transferring from the cloud is
--File storage: Up to 1TB is ¥ 2.45 / GB, --Transfer from the cloud: ¥ 0.37 per 100,000 times
It's like that. I think it's cheap.
Reference: Azure Storage Pricing
Upload images from Android to Azure Blob Storage.
--Microsoft Azure account with subscription enabled --Android Studio (I used 2.3)
// Since it is a basic part, the code is omitted
The id should be ** button **.
Click the ** + ** button in the upper right and enter ** Blob ** in the search box that appears to suggest Azure Blob Storage. Click on it.
Give it a unique name for all users in Azure. Give the resource group a unique name for each individual user. Subscriptions cannot be created with subscriptions provided by Imagine, so you can also participate in MSP-sponsored events etc. ** Azure Pass ** Please use etc.
Finally, click ** Pin to Dashboard ** and click Finish to complete the setting.
I think the following screen will open.
Let's open the ** Blob ** part of the service category
You may have received a message that the container has not been created yet.
Container ... what is it like?
Now let's take a look at how Azure Blob Storage works.
Quoted from Using Azure Blob Storage with .NET
It may be hard to understand at first glance, so why not replace it with the computer terminology you are used to?
Account = USB memory Container = Folder in USB memory BLOB = Image collection and video collection placed in the folder
I feel like you understand, right?
Create a container from the button on the upper left.
One thing to keep in mind when naming Azure Blob Storage containers, they must be all lowercase
Also, the access type should be Blob
Azure is now ready.
Open ** MainActivity.java ** on Android. Then enter the code below
//import omitted
public class MainActivity extends AppCompatActivity {
public Button Button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button = (Button) findViewById(R.id.button);
Button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//If you execute heavy processing using the network in the UI thread, it will cause trouble, so execute it in another thread
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
//String for connecting with Azure Blob Storage, Cho important. It's as important as the password for the Root account.
String storageConnectionString = "Connection String";
//Get the path to DCIM
File dir = new File(Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM);
//Set the path to the image file on DCIM
File file = new File(dir.getAbsolutePath() + "/image.jpeg ");
//Log output to check if the path to the correct image is taken
Log.d("The one who displays the image", file.getPath());
try {
//Start connecting to Azure Storage Account
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
//Get a client to connect to a blob in your Azure Storage Account
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
//Get the blob container, specify the container as a string
CloudBlobContainer container = blobClient.getContainerReference("mspjp");
//The character string passed in the argument of getBlockBlobReference becomes the file name saved in Blob.
//If a file with the same name already exists, it will be overwritten
CloudBlockBlob blob = container.getBlockBlobReference("myimage.jpg ");
//Exit the function if the file does not exist
if (!file.exists()) return null;
//Upload to Azure Blob Storage
blob.upload(new java.io.FileInputStream(file), filelength());
} catch (Exception e) {
//Spits a stack trace for analysis if an error occurs
e.printStackTrace();
}
return null;
}
};
try {
//Run in a separate thread
task.execute();
} catch (Exception e) {
Handler handler = new Handler();
handler.post(new Runnable() {
//I want to display an error message on toast when an error occurs,
//I'm running in the UI thread because I can't see the toast unless it's in the UI thread
@Override
public void run() {
Toast.makeText(MainActivity.this, "Faild: Upload blob storage", Toast.LENGTH_LONG).show();
}
});
}
}
});
}
}
Connect Android to your PC, open the external storage of Android, and put the image file ** image.jpeg ** in the folder ** DCIM ** in it.
The storageConnectionString is different for each user (Storage Account), so let's get it now.
First, open the Azure Blob Storage screen you created earlier.
Click on the part labeled Access Key
Rewrite the code to copy the connection string of ** key1 ** and assign it to ** storageConnectionString **
Now that the coding is complete, try running it.
After running, open the Azure Blob Storage container and you'll see the image uploaded.
--Java fun ――Since Azure has all the documents, I felt that it was easy to solve even if I didn't understand.
Recommended Posts