合并多个Azure';Android中的s云块Blob
我是Azure Blob云的新手。我基本上想从我的android应用程序上传一个视频文件到Azure云,但我不能,因为一旦大小达到32MB,它就会停止,并抛出一个异常作为OutOfMemory。因此,我做了一些研究,研究如何解决这个问题,我提出了一个解决方案,将一个文件分解成字节,然后作为多个blob上传。最后,将它们编译成一个blob。但我不知道怎么做。我试着使用commitBlockList,但为此我无法获得每个blob的Id合并多个Azure';Android中的s云块Blob,android,azure,azure-storage-blobs,Android,Azure,Azure Storage Blobs,我是Azure Blob云的新手。我基本上想从我的android应用程序上传一个视频文件到Azure云,但我不能,因为一旦大小达到32MB,它就会停止,并抛出一个异常作为OutOfMemory。因此,我做了一些研究,研究如何解决这个问题,我提出了一个解决方案,将一个文件分解成字节,然后作为多个blob上传。最后,将它们编译成一个blob。但我不知道怎么做。我试着使用commitBlockList,但为此我无法获得每个blob的Id try { // Setup the cloud
try {
// Setup the cloud storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
int maxSize = 64 * Constants.MB;
// Create a blob service client
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
blobClient.getDefaultRequestOptions().setSingleBlobPutThresholdInBytes(maxSize);
CloudBlobContainer container = blobClient.getContainerReference("testing");
container.createIfNotExists();
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
container.uploadPermissions(containerPermissions);
CloudBlockBlob finalFile = container.getBlockBlobReference("1.jpg");
CloudBlob b = container.getBlockBlobReference("temp");
String Lease = b.getSnapshotID();
b.uploadFromFile(URL);
List<BlockEntry> blockEntryIterator = new ArrayList<>();
blockEntryIterator.add(new BlockEntry(Lease));
finalFile.commitBlockList(blockEntryIterator);
} catch (Throwable t) {
}
试试看{
//设置云存储帐户。
CloudStorageAccount-storageAccount=CloudStorageAccount.parse(storageConnectionString);
int maxSize=64*Constants.MB;
//创建blob服务客户端
CloudBlobClient blobClient=storageAccount.createCloudBlobClient();
blobClient.getDefaultRequestOptions().setSingleBlobPutThresholdInBytes(maxSize);
CloudBlobContainer容器=blobClient.getContainerReference(“测试”);
container.createIfNotExists();
BlobContainerPermissions containerPermissions=新BlobContainerPermissions();
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
container.uploadPermissions(containerPermissions);
CloudBlockBlob finalFile=container.getBlockBlobReference(“1.jpg”);
CloudBlob=container.getBlockBlobReference(“temp”);
String Lease=b.getSnapshotID();
b、 上传文件(URL);
List blockEntryIterator=new ArrayList();
blockEntryIterator.add(新块条目(租赁));
最终文件提交锁列表(blockEntryIterator);
}捕获(可丢弃的t){
}
更新~~~
我试图将文件分解为多个部分,但现在出现以下错误“指定的blob或块内容无效”。
public void splitTest(字符串URL)引发IOException、URISyntaxException、InvalidKeyException、StorageException{
new ConversionNotificationSetup().sendNotification(a.getApplicationContext(),"STARTED");
CloudBlockBlob blob = null;
List<BlockEntry> blockList = null;
try{
// get file reference
FileInputStream fs = new FileInputStream( URL );
File sourceFile = new File( URL);
// set counters
long fileSize = sourceFile.length();
int blockSize = 3 * (1024 * 1024); // 256K
int blockCount = (int)((float)fileSize / (float)blockSize) + 1;
long bytesLeft = fileSize;
int blockNumber = 0;
long bytesRead = 0;
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference("testing");
String title = "Android_" + getFileNameFromUrl(URL);
// get ref to the blob we are creating while uploading
blob = container.getBlockBlobReference(title);
blob.deleteIfExists();
// list of all block ids we will be uploading - need it for the commit at the end
blockList = new ArrayList<BlockEntry>();
// loop through the file and upload chunks of the file to the blob
while( bytesLeft > 0 ) {
blockNumber++;
// how much to read (only last chunk may be smaller)
int bytesToRead = 0;
if ( bytesLeft >= (long)blockSize ) {
bytesToRead = blockSize;
} else {
bytesToRead = (int)bytesLeft;
}
// trace out progress
float pctDone = ((float)blockNumber / (float)blockCount) * (float)100;
// save block id in array (must be base64)
String x = "";
if(blockNumber<=9) {
traceLine( "blockid: 000" + blockNumber + ". " + String.format("%.0f%%",pctDone) + " done.");
x = "blockid000" + blockNumber;
}
else if(blockNumber>=10 && blockNumber<=99){
traceLine( "blockid: 00" + blockNumber + ". " + String.format("%.0f%%",pctDone) + " done.");
x = "blockid00" + blockNumber;
}
else if(blockNumber>=100 && blockNumber<=999){
traceLine( "blockid0: " + blockNumber + ". " + String.format("%.0f%%",pctDone) + " done.");
x = "blockid0" + blockNumber;
}
else if(blockNumber>=1000 && blockNumber<=9999){
traceLine( "blockid: " + blockNumber + ". " + String.format("%.0f%%",pctDone) + " done.");
x = "blockid" + blockNumber;
}
String blockId = Base64.encodeToString(x.getBytes(),Base64.DEFAULT).replace("\n","").toLowerCase();
traceLine( "Base 64["+x+"] -> " + blockId);
BlockEntry block = new BlockEntry(blockId);
blockList.add(block);
// upload block chunk to Azure Storage
blob.uploadBlock( blockId, fs, (long)bytesToRead);
// increment/decrement counters
bytesRead += bytesToRead;
bytesLeft -= bytesToRead;
}
fs.close();
traceLine( "CommitBlockList. BytesUploaded: " + bytesRead);
blob.commitBlockList(blockList);
new ConversionNotificationSetup().sendNotification(a.getApplicationContext(),"UPLOAD COMPLETE");
return;
}
catch (StorageException storageException) {
traceLine("StorageException encountered: ");
traceLine(storageException.getMessage());
new ConversionNotificationSetup().sendNotification(a.getApplicationContext(),"FAILED");
assert blockList != null;
blob.commitBlockList(blockList);
return;
} catch( IOException ex ) {
traceLine( "IOException: " + ex );
new ConversionNotificationSetup().sendNotification(a.getApplicationContext(),"FAILED");
assert blockList != null;
blob.commitBlockList(blockList);
return;
} catch (Exception e) {
traceLine("Exception encountered: ");
traceLine(e.getMessage());
new ConversionNotificationSetup().sendNotification(a.getApplicationContext(),"FAILED");
assert blockList != null;
blob.commitBlockList(blockList);
return;
}
}
new ConversionNotificationSetup().sendNotification(a.getApplicationContext(),“已启动”);
CloudBlockBlob blob=null;
List blockList=null;
试一试{
//获取文件引用
FileInputStream fs=新的FileInputStream(URL);
文件源文件=新文件(URL);
//设置计数器
long fileSize=sourceFile.length();
int blockSize=3*(1024*1024);//256K
int blockCount=(int)((float)fileSize/(float)blockSize)+1;
long bytesLeft=文件大小;
int blockNumber=0;
长字节读取=0;
CloudStorageAccount-storageAccount=CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient blobClient=storageAccount.createCloudBlobClient();
CloudBlobContainer容器=blobClient.getContainerReference(“测试”);
String title=“安卓”+getFileNameFromUrl(URL);
//获取上传时创建的blob的引用
blob=container.getBlockBlobReference(标题);
blob.deleteIfExists();
//我们将要上载的所有块ID的列表-在最后提交时需要它
blockList=newarraylist();
//循环浏览文件并将文件块上载到blob
while(字节左>0){
blockNumber++;
//读取多少(只有最后一个块可能更小)
int bytesToRead=0;
if(字节左>=(长)块大小){
bytesToRead=块大小;
}否则{
bytesToRead=(int)bytesleeft;
}
//追踪进展
float pctDone=((float)blockNumber/(float)blockCount)*(float)100;
//在数组中保存块id(必须是base64)
字符串x=“”;
如果(blockNumber=10&&blockNumber=100&&blockNumber=1000&&blockNumber=1000&&blockNumber)并逐字节读取,则可以使用此选项。我可以上载~1GB的文件,但在此之后会出现错误500并崩溃。如果有人有任何解决方案,请务必让我知道
public boolean splitTest(String URL) throws IOException, URISyntaxException, InvalidKeyException, StorageException {
new ConversionNotificationSetup().sendNotification(a.getApplicationContext(),"STARTED");
CloudBlockBlob blob = null;
List<BlockEntry> blockList = null;
try{
// get file reference
FileInputStream fs = new FileInputStream(URL);
File sourceFile = new File(URL);
// set counters
long fileSize = sourceFile.length();
int blockSize = 512 * 1024; // 256K
//int blockSize = 1 * (1024 * 1024); // 256K
int blockCount = (int)((float)fileSize / (float)blockSize) + 1;
long bytesLeft = fileSize;
int blockNumber = 0;
long bytesRead = 0;
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference("testing");
String title = "Android/Android_" + getFileNameFromUrl(URL).replace("\n","").replace(" ","_").replace("-","").toLowerCase();
// get ref to the blob we are creating while uploading
blob = container.getBlockBlobReference(title);
traceLine("Title of blob -> " + title);
if(blob.exists())
blob.deleteIfExists();
blob.setStreamWriteSizeInBytes(blockSize);
// list of all block ids we will be uploading - need it for the commit at the end
blockList = new ArrayList<>();
// loop through the file and upload chunks of the file to the blob
while( bytesLeft > 0 ) {
// how much to read (only last chunk may be smaller)
int bytesToRead = 0;
if ( bytesLeft >= (long)blockSize ) {
bytesToRead = blockSize;
} else {
bytesToRead = (int)bytesLeft;
}
// trace out progress
float pctDone = ((float)blockNumber / (float)blockCount) * (float)100;
// save block id in array (must be base64)
String x = "";
if(blockNumber<=9) {
traceLine( "tempblobid0000" + blockNumber + ". " + String.format("%.0f%%",pctDone) + " done.");
x = "tempblobid0000" + blockNumber;
}
else if(blockNumber>=10 && blockNumber<=99){
traceLine( "tempblobid000" + blockNumber + ". " + String.format("%.0f%%",pctDone) + " done.");
x = "tempblobid000" + blockNumber;
}
else if(blockNumber>=100 && blockNumber<=999){
traceLine( "tempblobid00" + blockNumber + ". " + String.format("%.0f%%",pctDone) + " done.");
x = "tempblobid00" + blockNumber;
}
else if(blockNumber>=1000 && blockNumber<=9999){
traceLine( "tempblobid0" + blockNumber + ". " + String.format("%.0f%%",pctDone) + " done.");
x = "tempblobid0" + blockNumber;
}
else if(blockNumber>=10000 && blockNumber<=99999){
traceLine( "tempblobid" + blockNumber + ". " + String.format("%.0f%%",pctDone) + " done.");
x = "tempblobid" + blockNumber;
}
String blockId = Base64.encodeToString(x.getBytes(),Base64.NO_WRAP).replace("\n","").toLowerCase();
traceLine( "Base 64["+ x +"] -> " + blockId);
BlockEntry block = new BlockEntry(blockId);
blockList.add(block);
// upload block chunk to Azure Storage
blob.uploadBlock( blockId, fs, (long)bytesToRead);
notification2(a,pctDone);
//a.update(pctDone);
// increment/decrement counters
bytesRead += bytesToRead;
bytesLeft -= bytesToRead;
blockNumber++;
}
fs.close();
traceLine( "CommitBlockList. BytesUploaded: " + bytesRead + "\t total bytes -> " + fileSize + "\tBytes Left -> " + bytesLeft);
blob.commitBlockList(blockList);
new ConversionNotificationSetup().sendNotification(a.getApplicationContext(),"UPLOAD COMPLETE");
return true;
}
catch (StorageException storageException) {
traceLine("StorageException encountered: ");
traceLine(storageException.getMessage());
traceLine("HTTP Status code -> " + storageException.getHttpStatusCode());
new ConversionNotificationSetup().sendNotification(a.getApplicationContext(),"FAILED");
if (blob != null) {
blob.commitBlockList(blockList);
}
return false;
} catch( IOException ex ) {
traceLine( "IOException: " + ex );
new ConversionNotificationSetup().sendNotification(a.getApplicationContext(),"FAILED");
if (blob != null) {
blob.commitBlockList(blockList);
}
return false;
} catch (Exception e) {
traceLine("Exception encountered: ");
traceLine(e.getMessage());
new ConversionNotificationSetup().sendNotification(a.getApplicationContext(),"FAILED");
if (blob != null) {
blob.commitBlockList(blockList);
}
return false;
}
}
public boolean splitTest(字符串URL)抛出IOException、URISyntaxException、InvalidKeyException、StorageException{
新建转换NotificationSetup().sendNotification(a.getApplicationContext(),“已启动”);
CloudBlockBlob blob=null;
List blockList=null;
试一试{
//获取文件引用
FileInputStream fs=新的FileInputStream(URL);
文件源文件=新文件(URL);
//设置计数器
long fileSize=sourceFile.length();
int blockSize=512*1024;//256K
//int blockSize=1*(1024*1024);//256K
int blockCount=(int)((float)fileSize/(float)blockSize)+1;
long bytesLeft=文件大小;
int blockNumber=0;
长字节读取=0;
CloudStorageAccount-storageAccount=CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient blobClient=storageAccount.createCloudBlobClient();
CloudBlobContainer容器=blobClient.getContainerReference(“测试”);
String title=“Android/Android_”+getFileNameFromUrl(URL).replace(“\n”,”).replace(“,”).replace(“,”).replace(“-”,”).toLowerCase();
//获取上传时创建的blob的引用
blob=container.getBlockBlobReference(标题);
轨迹线(“水滴的标题->”+标题);
if(blob.exists())
blob.deleteIfExists();
blob.setStreamWriteSizeInBytes(块大小);
//我们将要上载的所有块ID的列表-在最后提交时需要它
blockList=newarraylist();
//循环浏览文件并将文件块上载到blob
while(字节左>0){
//读取多少(只有最后一个块可能更小)
int bytesToRead=0;
if(字节左>=(长)块大小){
bytesToRead=块大小;
}否则{
bytesToRead=(int)bytesleeft;
}
//追踪进展