简体   繁体   中英

get GCS file metadata using scala

I want to get the time creation of files in GCS, I used the code below:

println(Files
  .getFileAttributeView(Paths.get("gs://datalake-dev/mu/tpu/file.0450138"), classOf[BasicFileAttributeView])
  .readAttributes.creationTime)

The problem is that the Paths.get function replace // with / so I will get gs:/datalake-dev/mu/tpu/file.0450138 instead of gs://datalake-dev/mu/tpu/file.0450138 .

Anyone can help me with this?

Thanks a lot !

I solved the problem by adding the following java code and then calling the java function in scala.

import com.google.cloud.storage.*;
import java.sql.Timestamp;

public class ExtractDate {
    public static String getTime(String fileName){
        String bucketName = "bucket-data";
        String blobName = "doc/files/"+fileName;
        // Instantiates a client
        Storage storage_client = StorageOptions.getDefaultInstance().getService();
        Bucket bucket = storage_client.get(bucketName);
        //val storage_client = Storage.
        BlobId blobId = BlobId.of(bucketName, blobName);
        Blob blob =  storage_client.get(blobId);
        Timestamp tmp = new Timestamp(bucket.get(blobName).getCreateTime());
        System.out.print(bucket.get(blobName).getContent());
       // return the year of the file date creation 
        return tmp.toString().substring(0,4);
    }
}

You can use the file_get_contents method to read the contents of the path. From the documentation on Reading and Writing Files

Read objects contents using PHP to fetch an object's custom metadata from Google Cloud Storage.An App Engine PHP 5 app must use the Cloud Storage stream wrapper to write files at runtime. However, if an app needs to read files, and these files are static, you can optionally read static files uploaded with your app using PHP filesystem functions such as file_get_contents.

$fileContents = file_get_contents($filePath);

where the path specified must be a path relative to the script accessing them.

You must upload the file or files in an application subdirectory when you deploy your app to App Engine, and must configure the app.yaml file so your app can access those files. For complete details, see PHP 5 Application Configuration with app.yaml .

In the app.yaml configuration, notice that if you use a static file or directory handler (static_files or static_dir) you must specify application_readable set to true or your app won't be able to read the files. However, if the files are served by a script handler, this isn't necessary, because these files are readable by script handlers by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM