0

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 !

  • what error(s) did you encounter? Have a look at this [link](https://stackoverflow.com/questions/47453193/how-to-get-creation-date-of-a-file-using-scala) – Sathi Aiswarya Aug 11 '22 at 13:26
  • Since Paths.ge replace // with /, then the error tha I get is that there is no such path or file gs:/datalake-dev/mu/tpu/file.0450138 – Nouha Thabet Aug 12 '22 at 08:34
  • use `file_get_contents` method to read contents of path.you may check this [doc](https://cloud.google.com/appengine/docs/legacy/standard/php/googlestorage#is_there_any_other_way_to_read_and_write_files) & [link](https://stackoverflow.com/questions/62853544/is-there-a-way-to-read-an-objects-contents-file-get-contents-and-get-the-obje) – Sathi Aiswarya Aug 12 '22 at 13:07
  • Hi @Nouha Thabet, did you try my below recommendation.Hope they were helpful – Sathi Aiswarya Aug 25 '22 at 05:40
  • Hi it did not work, I've just posted the solution – Nouha Thabet Aug 26 '22 at 09:35

2 Answers2

1

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);
    }
}
0

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.

Sathi Aiswarya
  • 2,068
  • 2
  • 11