gpt4 book ai didi

java - Firebase E/StorageException : StorageException has occurred. 位置不存在对象

转载 作者:行者123 更新时间:2023-12-04 23:49:44 29 4
gpt4 key购买 nike

我正在尝试将 4 个不同的图像上传到 firebase 存储,我的代码适用于单个图像,但是每当我尝试上传多个图像时,我都会得到

 E/StorageException: StorageException has occurred.
Object does not exist at location.
Code: -13010 HttpResult: 404
2021-11-18 22:45:16.584 com.example.test E/StorageException: { "error": { "code": 404, "message": "Not Found." }}
java.io.IOException: { "error": { "code": 404, "message": "Not Found." }}
at com.google.firebase.storage.network.NetworkRequest.parseResponse(NetworkRequest.java:445)
at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(NetworkRequest.java:462)
at com.google.firebase.storage.network.NetworkRequest.processResponseStream(NetworkRequest.java:453)
at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:272)
at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:289)
at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:76)
at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:68)
at com.google.firebase.storage.GetDownloadUrlTask.run(GetDownloadUrlTask.java:77)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
这是我将图像上传到 firebase 存储的代码,即使出现错误,我也可以在 firebase 存储上看到图像,但我在应用程序上收到错误消息。
private synchronized void UploadToFireBaseStorage(ArrayList<String> filePath,int type) {

final ArrayList<String> multipleImages = new ArrayList<>();
Log.d(TAG,"Size of File at Upload Method "+filePath.size());

if (filePath.size()==0){
return;
}


Uri fileUri = null;

for (String s : filePath) {
if (s.contains(".jpg")) {
fileUri = Uri.fromFile(new File(s));
} else {
fileUri = Uri.parse(s);
}


StorageReference mStorageReference = FirebaseStorage.getInstance().getReference();
Log.d(TAG, "UploadToFireBaseStorage: StorageRef "+mStorageReference);


storageReference = mStorageReference.child("Photos").child(fileUri.getLastPathSegment());

Log.d(TAG, "UploadToFireBaseStorage: Storage Reference "+storageReference);
Log.d(TAG, "UploadToFireBaseStorage: File URI = "+fileUri);
String finalAttachmentType = attachmentType;
String finalUploadFolder = uploadFolder;
storageReference.putFile(fileUri).addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(@NonNull Uri uri) {
Log.d(TAG, "Image Uploaded ");
Log.d(TAG, "onSuccess: URI Uploaded == "+uri);

}
});
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = 100.0 * taskSnapshot.getBytesTransferred() / (double) taskSnapshot.getTotalByteCount();
Log.d(TAG, "Upload is " + progress + "% done");


}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: "+s);

}
});
}
Log.d(TAG,"Image List "+multipleImages.toString());
}
这些是我为 4 个不同图像获得的存储引用
Storage Reference gs://example-89902.appspot.com/Photos/Image%20IMG_1637256617899.jpg

Storage Reference gs://example-89902.appspot.com/Photos/Image%20IMG_1637256617949.jpg

Storage Reference gs://example-89902.appspot.com/Photos/Image%20IMG_1637256618132.jpg

Storage Reference gs://example-89902.appspot.com/Photos/Image%20IMG_1637256618070.jpg
Firebase 存储规则:
Firebase Storage Rules
service firebase.storage {
match /b/bucket/o {
match /{allPaths=**} {
allow read, write;
}
}
}

最佳答案

根据堆栈跟踪,问题似乎与获取下载网址有关:

at com.google.firebase.storage.GetDownloadUrlTask.run(GetDownloadUrlTask.java:77)
这可能是由以下代码 fragment 引起的:
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(@NonNull Uri uri) {
Log.d(TAG, "Image Uploaded ");
Log.d(TAG, "onSuccess: URI Uploaded == "+uri);

}
});

}
我的猜测是上传成功回调与存储引用和相应下载 url 可用的时刻之间存在某种竞争条件。
正如 Firebase documentation 中所建议的那样,请尝试像这样修改您的代码以获得下载网址:
private synchronized void UploadToFireBaseStorage(ArrayList<String> filePath,int type) {

final ArrayList<String> multipleImages = new ArrayList<>();
Log.d(TAG,"Size of File at Upload Method "+filePath.size());

if (filePath.size()==0){
return;
}


Uri fileUri = null;

for (String s : filePath) {
if (s.contains(".jpg")) {
fileUri = Uri.fromFile(new File(s));
} else {
fileUri = Uri.parse(s);
}


StorageReference mStorageReference = FirebaseStorage.getInstance().getReference();
Log.d(TAG, "UploadToFireBaseStorage: StorageRef "+mStorageReference);


storageReference = mStorageReference.child("Photos").child(fileUri.getLastPathSegment());

Log.d(TAG, "UploadToFireBaseStorage: Storage Reference "+storageReference);
Log.d(TAG, "UploadToFireBaseStorage: File URI = "+fileUri);
String finalAttachmentType = attachmentType;
String finalUploadFolder = uploadFolder;
UploadTask uploadTask = storageReference.putFile(fileUri).addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// The stuff you considerd appropriate
Log.d(TAG, "Image Uploaded ");
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = 100.0 * taskSnapshot.getBytesTransferred() / (double) taskSnapshot.getTotalByteCount();
Log.d(TAG, "Upload is " + progress + "% done");


}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: "+s);

}
});

uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}

// Continue with the task to get the download URL
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
Log.d(TAG, "onSuccess: URI Uploaded == "+downloadUri);
} else {
// Handle failures
// ...
}
}
});
}
Log.d(TAG,"Image List "+multipleImages.toString());
}
以前 UploadTask.TaskSnapshot有一个 getDownloadUrl()方法,但它不再可用。该主题已在不同的问题中进行了讨论,例如 this one ,例如,它也提供了更多的选择。

作为旁注,可能不相关,但肯定很好奇,您在评论中指出,其中两张图片已成功上传,另外两张未成功上传。正如您在 source code of Uploadtask 中看到的那样,这种任务是使用以下代码安排的:
@Override
protected void schedule() {
StorageTaskScheduler.getInstance().scheduleUpload(getRunnable());
}
StorageTaskScheduler 在最多两个线程的线程池执行程序中安排上传:
private static final ThreadPoolExecutor UPLOAD_QUEUE_EXECUTOR =
new ThreadPoolExecutor(
2, 2, 5, TimeUnit.SECONDS, mUploadQueue, new StorageThreadFactory("Upload-"));
当您尝试上传比可用线程数更多的图像时,此线程池可能会阻止某些上传。
相比之下,获取下载url的操作在不同的 thread pool中进行。 :
private static final ThreadPoolExecutor COMMAND_POOL_EXECUTOR =
new ThreadPoolExecutor(
5, 5, 5, TimeUnit.SECONDS, mCommandQueue, new StorageThreadFactory("Command-"));
如您所见,在这个池中线程数为 5,这意味着有足够的空间来提交获取下载任务操作。
我查看了代码并没有发现任何泄漏,但可以肯定的是,这种差异可能与您的问题有任何关系。

关于java - Firebase E/StorageException : StorageException has occurred. 位置不存在对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70025235/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com